Second minimum date in power bi - date

I have some users with different game_id.
for each user, I want to find the second minimum date. (column: min2_date)
If a user doesn't have a second date (look at user_id: 2, in this example), his min2_date should be -1.
If the second minimum date is the same as the first minimum date(look at user_id: 4), we should write that date in the min2_date column.
I don't know How I should calculate the second minimum date in Power BI.
please help me if you know.

Because you dont want a real second min date (but date in second row by order), we must try some trickyway.
One of way that we can do that is:
MinDate2 =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, -1,
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)
where MinDate and MinDate2 are measures.
AS a calculatedColumn:
MinDate2_col =
var _countrows = CALCULATE(countrows(VALUES(games[dates])), ALL(games[dates]) )
return
if(_countrows = 1, "-1",
FORMAT(DISTINCT(TOPN(1,TOPN(2,CALCULATETABLE(SELECTCOLUMNS(games, "dates",games[dates]), ALL(games[dates])),[dates], asc), [dates], desc)), "yyyy-mm-dd")
)

Related

DAX Calculate Billing Days Between Two Variable Dates

I have a dimdate table that is represented below. I have each day flagged as BusinessDay Y/N. I also have a DimSalesRep table that has a daily goal for each rep. I want to be able to allow users to input a StartDt and EndDt with filters on the report and have a calculated column look at the business days between those dates. I can calculate daysbetween with defined dates but I am unsure how I would use DAX with variable dates that are applied through Report filters.
I should also note I am not sure how best to handle a startdt and enddt filter based of the column, TheDate
Cheers!
Reference your dimdate table twice
StartDate = 'dimdate'
EndDate = 'dimdate'
and use this measure:
Num BusinessDays =
CALCULATE(
COUNTROWS('dimdate'),
'dimdate'[BusinessDay] = "Y",
'dimdate'[Date] >= SELECTEDVALUE(StartDate[Date]),
'dimdate'[Date] <= SELECTEDVALUE(EndDate[Date])
)

Display Data between Dec Y-1 and Dec current Y in PowerBI

I am new in powerBI, I loonking to display data in during a closing period and in my case it is from :
31/12/Y-1 to 31/12/Y my issue it is with slicer the year filter all date in the current year and not taking into account the value in 31/12/Y-1
How can I set PowerBI to do so,
thanks for your support
Display Sum of data in a defined period 31/12/Y-1 till 31/12/Y, user can select the period that they want to display and the data will update via PowerBI
Slicer below :
Use this calculated column
Closing Period =
VAR thisYear = YEAR(TODAY())
VAR startDate = DATE(thisYear -1 , 12, 31)
VAR endDate = DATE(thisYear, 12, 31)
RETURN
IF(startDate <= 'Date'[Date] && 'Date'[Date] <= endDate, thisYear)
and filter the 'Date'[Closing Period] column on the current year.
You can use a new calculated column for the slicer in your case:
Filter_date = DATEADD ( 'Date Table'[date].[Date], +1, DAY )

Rolling average a certain period using a DAX measure in Power BI

I tried generating a DAX measure as a rolling average. I don't quite know how to insert the measure I need rolling averaged as "CPI_annualized". It is not giving an option to bring CPI_annualized into the Rolling Average calc.
This is the error I am given when trying to construct the RollingAverage VAR in the measure P_MA
The syntax for 'CALCULATE' is incorrect. DAX(VAR __LAST_DATE = LASTDATE('public econometrics'[date]) ......
This is the DAX measure that I am trying to complete :
P_MA =
VAR __NUM_PERIODS = 3
VAR __LAST_DATE = LASTDATE('public econometrics'[date])
VAR RollingAverage =
AVERAGEX(
DATESBETWEEN(
'public econometrics'[date],
DATEADD(__LAST_DATE, -__NUM_PERIODS, MONTH),
__LAST_DATE)
CALCULATE([CPILFESL]))
)
RETURN RollingAverage
This is the DAX measure that I am trying to use in the rolling calculation with the data in my dB given monthly. This works as below.
CPI_annualized = (CALCULATE(SUM('public econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL")/CALCULATE(SUM('public
econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL",SAMEPERIODLASTYEAR('public econometrics'[date])))-1
Inserting this measure in a line chart gives my this table.
date
CPILFESL
1 January, 1978
6.41%
1 February, 1978
6.20%
I think you are getting error message due to this line:
CALCULATE([CPILFESL])
In Dax Calculate measure, you cannot directly calculate a value, instead you have to include a number calculation function such as sum, max, min, here is the example:
CALCULATE(sum([CPILFESL]))
In addition, you can get rid of Calculate measure when there is no filter expression, such as sum([CPILFESL]) is enough.
A proper way of using calculate is to filter the value, as shown in online documentation
CALCULATE(
SUM(Sales[Sales Amount]),
'Product'[Color] = "Blue"
)
You can find many usefull templated here:
https://www.daxpatterns.com/month-related-calculations/
Moving average 3 months
VAR MonthsInRange = 3
VAR LastMonthRange =
MAX ( 'Date'[Year Month Number] )
VAR FirstMonthRange =
LastMonthRange - MonthsInRange + 1
VAR Period3M =
FILTER (
ALL ( 'Date'[Year Month Number] ),
'Date'[Year Month Number] >= FirstMonthRange
&& 'Date'[Year Month Number] <= LastMonthRange
)
VAR Result =
IF (
COUNTROWS ( Period3M ) >= MonthsInRange,
CALCULATE (
AVERAGEX ( Period3M, [Sales Amount] ),
REMOVEFILTERS ( 'Date' )
)
)
RETURN
Result

Create calculated column to add + work days to a date

I am trying to create a calculated column to add + X work days to a date based on a work day steering table.
In the work day steering table, the bank days are flagged as 0.
What DAX formula should I use to create the calculated column and shift the date + work days further?
Expected Result:
Work day steering table:
You can use either a Measure or Calculated Column code as given below-
Measure Code
add_day_dinamically =
MAXX(
TOPN(
MIN(your_table_name[transport_lead_time]),
FILTER(
all(work_day_steering_table),
work_day_steering_table[flag] = 1
&& work_day_steering_table[date].[Date] > MIN(your_table_name[date_column_name])
),
work_day_steering_table[date].[Date],
ASC
),
work_day_steering_table[date].[Date]
)
Calculated Column Code
add_day_dinamically_column =
MAXX(
TOPN(
your_table_name[transport_lead_time],
FILTER(
all(work_day_steering_table),
work_day_steering_table[flag] = 1
&& work_day_steering_table[date].[Date] > your_table_name[date_column_name].[Date]
),
work_day_steering_table[date].[Date],
ASC
),
work_day_steering_table[date].[Date]
)
Here is the output-

Date query with the current date between two date_time columns

I have a fusion table with two date_time columns. The fist one is the start date (Startdatum) and in the other column is the end date (Einddatum).
I want to do a query with the current date, and only show the KML-lines on a map where the current date lies between the start and end date.
I tried to use the code below to create a string with a date format:
var time_date = new Date();
var day = time_date.getDate();
var month = time_date.getMonth()+1;
var year = time_date.getFullYear();
var date = (year+"."+month+"."+day);
To show the KML-lines on the map I tried to use the following code:
layer = new google.maps.FusionTablesLayer({
map: map,
heatmap: { enabled: false },
query: {
select: "col2",
from: "1mOMP1seJq4FdiNTugsfylZaJc8sKcSlfJKUuTJjv",
where: "'Startdatum' <= date AND 'Einddatum' >= date"
},
options: {
styleId: 2,
templateId: 2
}
});
Unfortunatly the map shows all the KMS-lines regardless what date is in one of the columns.
What am I doing wrong?
the where-clause is wrong, it has to be
where: "Startdatum <= '"+date+"' AND Einddatum >= '"+date+"'"
the date-format seems to be wrong. Although the used format yyyy.MM.dd is defined in the documentation, it doesn't work. The format yyyy-MM-dd currently works for me(but it's not defined in the documentation).
var date = (year+"-"+month+"-"+day);
(in case that day and month be less than 10 they wouldn't match the pattern, but that doesn't seem to be an issue)
Beyond that: when you fix these 2 mentioned parts it currently works(for me), but I've tried it a couple of hours ago and got unstable results.