DAX Convert date to the first day of quarter - powerbi

While building a calendar table I came across unexpected hard nut to crack.
How to convert date to the first date of the quarter? Here are some examples:
2019-02-01 > 2019-01-01
2019-03-31 > 2019-01-01
2019-04-02 > 2019-04-01
The function STARTOFQUARTER does not work in my calendar. I do not know why.
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016, 1, 1 ),
DATE ( 2020, 12, 31 )
),
VAR VarDates = [Date]
var YQ_date = STARTOFQUARTER( [Date] ) -- this does not work
RETURN
ROW (
"day" , VarDay,
"YQ_date" , YQ_date
)
)
The only option seems to be adding calculated column to Calendar table. But if it is possible to have it straight, then why not have it straight?

How about as an added column?
Calendar =
ADDCOLUMNS (
CALENDAR ( DATE ( 2016, 1, 1 ), DATE ( 2020, 12, 31 ) ),
"YQ_date", EOMONTH ( [Date], -1 - MOD ( MONTH ( [Date] ) - 1, 3 ) ) + 1
)

For the STARTOFQUARTER function to work, you need the dates that you expect to be returned by the function in your datetable.
So in the provided sample, you need to add the dates 2019-01-01 and 2019-04-01.
Something like this:

When you want to build a calendar table with one DAX expression, you cannot use the STARTOFQUARTER function, because it will not work with an in-memory table.
You could use something like this as a workaround though:
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016; 1; 1 );
DATE ( 2020; 12; 31 )
);
ROW (
"YQ_date"; DATE( Year( [Date] ); ROUNDUP( MONTH ( [Date] ) / 3; 0 ) * 3 - 2; 1)
)
)

Related

How to filter data for next week in dax?

I have a full DimDate dimension with all columns, and I try to filter data for ‘next week’.
I have tried:
'Date'[Week Year Week Number] = CONVERT(CONCATENATE( YEAR(TODAY() ),WEEKNUM(TODAY(),21) ),INTEGER)+1
But of course I have 2 problems:
when week is 52, I will just get 53.
Also the column Week Year Week Number has values like 202202 And my WEEKNUM concat returns something like 20222 (they are different!)
How can I appropriately filter for ‘next week’ data?
You can add days to TODAY with simple arithmetic: TODAY()+7. Then you can convert the year and weeknum without special logic.
You can always create a calendar table where the weeknums are continuous like this which would help in filtering for next week
Calendar=
VAR _cal1 =
CALENDAR ( DATE ( 2010, 1, 1 ), DATE ( 2020, 1, 1 ) )
VAR _cal2 =
ADDCOLUMNS (
_cal1,
"weekNum",
VAR _minDate = --- what is the min date in this calendar table
MINX ( _cal1, [Date] )
VAR _x =
WEEKDAY ( _minDate, 1 ) - 1
VAR _y = _minDate - _x
VAR _z =
CEILING ( DIVIDE ( ( [Date] - _y ), 7 ), 1 ) ---[Date] - _y gives the last Sunday before the minDate in the calendar
RETURN ---from when the WEEKNUM starts
_z
)
RETURN
_cal2

Need to display Last Month MTD on date based selection in the slicer power bi

I have a requirement in Power bi that i need to display the last month mtd on basis of selection of date slicer.
here is the details
Ex: jan 1 2020 : 10 cases, jan 2, 2020 : 30 cases.. when i select feb 2 2020 in slicer it should display the jan 2 2020 value not mtd of that month(january)
Thanks,
Imran
Without using time intelligence functions, you can probably write something like this:
LastMonthMTD =
VAR DateSelected = SELECTEDVALUE ( 'Calendar'[Date] )
VAR PrevMonth = EOMONTH ( DateSelected, -1 )
VAR PrevDate = DATE ( YEAR ( PrevMonth ), MONTH ( PrevMonth ), DAY ( DateSelected ) )
VAR StartDate = EOMONTH ( DateSelected, -2 )
RETURN
CALCULATE (
[Sum_Covid_Cases],
'Calendar'[Date] > StartDate,
'Calendar'[Date] <= PrevDate
)
Using time intelligence, you could try something like this:
LastMonthMTD =
CALCULATE (
[Sum_Covid_Cases],
DATEADD ( DATESMTD ( 'Calendar'[Date] ), -1, MONTH )
)
I haven't tested these, so let me know if they work or not.

Table of starting dates of each month in PBI

How to extract from Calendar table or how to generate from scratch a one column table containing only the first day of each month.
I can think of this:
SUMMARIZE(
'Calendar',
'Calendar'[Date].[Year],
'Calendar'[Date].[Month],
"minDate", MIN('Calendar'[Date])
)
Of that I need only the column minDate. Could there be any simpler or more efficient way to generate such series of starting month dates?
The problem with my query is that it is 3 columns, not one.
To generate from scratch:
first_day_of_month =
DISTINCT (
SELECTCOLUMNS (
SUMMARIZE (
CALENDAR ( DATE ( 2019, 1, 1 ), DATE ( 2019, 04, 01 ) ),
[Date],
"x", DATE ( YEAR ( [Date] ), MONTH ( [Date] ), 1 )
),
"first_day_of_month", [x]
)
)
A bit cleaner:
first_day_of_month =
GROUPBY (
SUMMARIZE (
CALENDAR ( DATE ( 2019; 1; 1 ); DATE ( 2019; 04; 01 ) );
[Date];
"first_day_of_month"; DATE ( YEAR ( [Date] ); MONTH ( [Date] ); 1 )
);
[first_day_of_month]
)
You can use SELECTCOLUMNS to return a table with only the column that you need.
SELECTCOLUMNS(
SUMMARIZE(
'Calendar',
'Calendar'[Date].[Year],
'Calendar'[Date].[Month],
"minDate", MIN('Calendar'[Date])
),
"minDate", [minDate])

DAX time intelligence functions with nested filters

I have a data structure like this
DateRoll Dataset Date Value Customer
Month Online 1/1/2018 10 Cust1
Month Online 2/1/2018 11 Cust1
Month Online 3/1/2018 12 Cust1
Month Online 4/1/2018 22 Cust1
Quarter Online 1/1/2018 33 Cust1
Quarter Online 4/1/2018 22 Cust1
I have to calculate previous quarter value, I tried different ways but it's not working
1 - Not returning any value.
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, QUARTER ),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter")
)
2--Nested - Returning overall total
CALCULATE (
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, QUARTER )
),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter" )
)
3--Nested --Returning overall total
CALCULATE (
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter" )
),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, MONTH )
)
Tried PREVIOUSQUARTER function too, but its not returning any value.
To take advantage of built in DAX time intelligence functions you will need to to have a contiguous set of dates. I would recommend using a date table. The following code can be used to create a date/calendar table in your model:
Celndar =
Var MinDate = MIN(Data_Rollup_KPI_DNR[Date])
Var MaxDate = MAX(Data_Rollup_KPI_DNR[Date])
Var BaseCalendar = CALENDAR(MinDate, MaxDate)
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
VAR YrMonth =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr
)
)
Once this table exists, mark it as a 'date' table and create a relationship with
Data_Rollup_KPI_DNR[Date]
Then, you can write the following measure to obtain the results you are searching for:
PQSum =
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
PREVIOUSQUARTER ( 'Calendar'[Date] )
)
Hope that helps!
*Edited
You can also create a ranking column to index in a measure:
Rank =
RANKX (
FILTER (
'Data_Rollup_KPI_DNR',
'Data_Rollup_KPI_DNR'[DateRoll] = EARLIER ( 'Data_Rollup_KPI_DNR'[DateRoll] )
),
'Data_Rollup_KPI_DNR'[Date].[Date],
,
ASC
)
Then you can reference a previous quarter using something like the following:
PQSum2 =
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
FILTER (
'Data_Rollup_KPI_DNR',
'Data_Rollup_KPI_DNR'[Rank]
= MAX ( 'Data_Rollup_KPI_DNR'[Rank] ) - 1
),
'Data_Rollup_KPI_DNR'[DateRoll] = "Quarter"
)
but this is hard coded and just plain nasty!
Echoing #steliok that a date dimension is the proper way to handle this; there are plenty of date table templates out there, and a date dimension will work with your data model. If you really really can't add to your data structure for some reason, this should work:
BaseValue = SUM ( 'Data_Rollup_KPI_DNR'[Value] )
PriorQuarter =
VAR CurrentDate = MAX ( 'Data_Rollup_KPI_DNR'[Date] )
VAR CurrentYear = YEAR ( CurrentDate )
VAR CurrentMonth = MONTH ( CurrentDate )
VAR FirstMonthOfCurrentQuarter =
SWITCH (
TRUE (),
CurrentMonth IN {1,2,3}, 1,
CurrentMonth IN {4,5,6}, 4,
CurrentMonth IN {7,8,9}, 7,
CurrentMonth IN {10,11,12}, 10
)
// DATE() does the right thing with negative month args
VAR PriorQuarterDate = DATE ( CurrentYear, FirstMonthOfCurrentQuarter - 3, 1 )
RETURN
CALCULATE (
[BaseValue],
ALL ( 'Data_Rollup_KPI_DNR'[DateRoll], 'Data_Rollup_KPI_DNR'[Date] ),
'Data_Rollup_KPI_DNR'[Date] = PriorQuarterDate,
'Data_Rollup_KPI_DNR'[DateRoll] = "Quarter"
)
This relies on DATE being clever, which it is. DATE ( 2019, -2, 1 ) = DATE ( 2018, 10, 1 ).
Ultimately, my question is why can't you just source the un-rolled up data from the same place that the ETL process is sourcing it?
Date functions are working well when you are using # Day level.
Following link would be helpful to resolve your issue,
https://community.powerbi.com/t5/Desktop/Lead-and-Lag-in-DAX/td-p/649162

Percentage of change with 2 data slicers in Power BI

I have a scenario with two data slicers. The first data slicer filters data for one period, the second one for another period. By editing visual interactions I got this works at the same page.
Now I want to compare two resulting values (in this case, number of transactions, and find a percentage of change between two selected periods.
I duplicated data column so I have two date columns for each slicer and I calculated the next measures:
# of Transactions 1 = CALCULATE(COUNT(Report[ProductID]),DATESBETWEEN(Report[Date1],[Start Date 1],[Last Date 1]))
# of Transactions 2 = CALCULATE(COUNT(Report[ProductID]),DATESBETWEEN(Report[Date2],[Start Date 2],[Last Date 2]))
% Transaction Change = ([# of Transactions 1]/[# of Transactions 2]) - 1
The first 2 measures are accurate (# of Transactions 1 & 2), but % of change doesn't work.
If you look at the screenshot below, you'll see # od Transactions 1 = 1,990 and # of Transactions 2 = 2,787. I want to compare this 2 values now.
How can I solve this?
Thank you.
First create two measure for your date bounds:
Min Date :=
MIN ( 'Report'[Date] )
Max Date :=
MAX ( 'Report'[Date] )
Then create a date table using the following DAX, this will join to you 'Report' table on the primary date:
Dates :=
VAR MinDate = [Min Date]
VAR MaxDate = [Max Date]
VAR BaseCalendar =
CALENDAR ( MinDate, MaxDate )
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
VAR YrMonth =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
VAR YrMonthQtr =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
& CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
VAR YrMonthQtrDay =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
& CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
& DAY ( BaseDate )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr,
"YrMonthQtr", YrMonthQtr,
"YrMonthQtrday", YrMonthQtrDay
)
)
Now create another date table from which to compare, and join to your primary date table in 'Report' and ensure the relationship is inactive:
Compare Dates :=
ALLNOBLANKROW ( 'Dates' )
Now create the [# of transaction] measure; one for 'Dates' and another for 'Compare Dates' like so:
[# of Transaction 1] :=
CALCULATE (
COUNT ( Report[ProductID] )
)
[# of Transaction 2] :=
CALCULATE (
[# of transaction 1],
ALL ( 'Dates' ),
USERELATIONSHIP ( 'Compare Dates'[Date], 'Report'[Date] )
)
Now Create the % Delta measure:
Transaction Change := CALCULATE(DIVIDE([# of Transactions 1],[# of Transactions 2]) - 1)
This should work like a charm and will work for any dates selected in your slicers, you will still need to associate your date slicers with your new date tables.
I hope this helps!!