Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.
Hi there,
I'm building a Calendar table in DAX for the first time. It needs to have columms for two different Financial Years :
UK - 1 April to 31 March
My Organisational FY 1 July - 31 June
Could someone help me with the DAX code for these two new colummns? Then I can add the columns for quarters. The Date field is just called Date. So I currently have, following the add column fuction, in a list, of other new columns:
Solved! Go to Solution.
Thanks for posting your existing code!
I have edited the code below, without wanting to change your code too much 🙂
The main points are:
Calendar =
ADDCOLUMNS (
CALENDAR ( DATE ( 2015, 1, 1 ), DATE ( 2049, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Quarter", QUARTER ( [Date] ),
"Month Num", MONTH ( [Date] ),
"Week Num", WEEKNUM ( [Date] ),
"Week Day", WEEKDAY ( [Date] ),
"Day", DAY ( [DATE] ),
"Month", FORMAT ( [Date], "Mmmm" ),
"Day Name", FORMAT ( [Date], "dddd" ),
"Quater Number", "Q" & QUARTER ( [Date] ),
"Year Month",
YEAR ( [Date] ) & " "
& FORMAT ( [Date], "Mmmm" ),
"Year Month Sort", YEAR ( [Date] ) & FORMAT ( MONTH ( [Date] ), "00" ),
"Year Quarter",
YEAR ( [Date] ) & " " & "Q"
& QUARTER ( [Date] ),
"Year Quarter Sort", YEAR ( [Date] ) & QUARTER ( [Date] ),
"1 Month to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -1 )
&& [Date] <= TODAY (),
TRUE ()
),
"3 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -3 )
&& [Date] <= TODAY (),
TRUE ()
),
"6 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -6 )
&& [Date] <= TODAY (),
TRUE ()
),
"12 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -12 )
&& [Date] <= TODAY (),
TRUE ()
),
"UK Financial Year",
VAR RefDate = EDATE ( [Date], 9 ) -- e.g. shifts Apr20-Mar21 => Jan-Dec 2021
VAR FY = YEAR ( RefDate )
RETURN
( FY - 1 ) & "/" & MOD ( FY, 100 ),
"Org Financial Year",
VAR RefDate = EDATE ( [Date], 6 ) -- e.g. shifts Jul20-Jun21 => Jan-Dec 2021
VAR FY = YEAR ( RefDate )
RETURN
( FY - 1 ) & "/" & MOD ( FY, 100 )
)
Does this work as intended?
Something like this is what I'm thinking:
Thanks for posting your existing code!
I have edited the code below, without wanting to change your code too much 🙂
The main points are:
Calendar =
ADDCOLUMNS (
CALENDAR ( DATE ( 2015, 1, 1 ), DATE ( 2049, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Quarter", QUARTER ( [Date] ),
"Month Num", MONTH ( [Date] ),
"Week Num", WEEKNUM ( [Date] ),
"Week Day", WEEKDAY ( [Date] ),
"Day", DAY ( [DATE] ),
"Month", FORMAT ( [Date], "Mmmm" ),
"Day Name", FORMAT ( [Date], "dddd" ),
"Quater Number", "Q" & QUARTER ( [Date] ),
"Year Month",
YEAR ( [Date] ) & " "
& FORMAT ( [Date], "Mmmm" ),
"Year Month Sort", YEAR ( [Date] ) & FORMAT ( MONTH ( [Date] ), "00" ),
"Year Quarter",
YEAR ( [Date] ) & " " & "Q"
& QUARTER ( [Date] ),
"Year Quarter Sort", YEAR ( [Date] ) & QUARTER ( [Date] ),
"1 Month to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -1 )
&& [Date] <= TODAY (),
TRUE ()
),
"3 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -3 )
&& [Date] <= TODAY (),
TRUE ()
),
"6 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -6 )
&& [Date] <= TODAY (),
TRUE ()
),
"12 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -12 )
&& [Date] <= TODAY (),
TRUE ()
),
"UK Financial Year",
VAR RefDate = EDATE ( [Date], 9 ) -- e.g. shifts Apr20-Mar21 => Jan-Dec 2021
VAR FY = YEAR ( RefDate )
RETURN
( FY - 1 ) & "/" & MOD ( FY, 100 ),
"Org Financial Year",
VAR RefDate = EDATE ( [Date], 6 ) -- e.g. shifts Jul20-Jun21 => Jan-Dec 2021
VAR FY = YEAR ( RefDate )
RETURN
( FY - 1 ) & "/" & MOD ( FY, 100 )
)
Does this work as intended?
This is almost perfect to something I'm looking for.
The FW StartOfMonth are the exact dates we work to, any idea how I would convert this into M language so I can use it in PowerQuery?
Hi Owen,
Yes that's fantastic. and that's for spotting the problem with Month Stort, probably saved me hours!
Adam
Hi Adam,
Below is an example of what I would normally do, written as a DAX query (if I were creating the table in DAX).
The part you are actually asking about is step 3 but I have included an overall description.
In order to map dates into financial years/months/quarters, I would:
Of course, this is just one method. I would also tend to prefer Power Query or further upstream to generate date tables, but the logic would be similar.
Have a look here for some further inspiration:
https://www.sqlbi.com/tools/dax-date-template/
EVALUATE
VAR StartDate = dt"2020-01-01"
VAR EndDate = dt"2023-12-31"
VAR UK_StartMonth = 4 -- UK Financial Year starts in April
VAR Org_StartMonth = 7 -- Org Financial Year starts in July
VAR UK_Offset =
MOD ( 13 - UK_StartMonth, 12 )
VAR Org_Offset =
MOD ( 13 - Org_StartMonth, 12 )
VAR BaseCalendar =
CALENDAR ( StartDate, EndDate )
VAR Result =
GENERATE (
BaseCalendar,
-- UK Financial Year Columns
VAR UK_ReferenceDate =
EDATE ( [Date], UK_Offset ) -- 9 in this case
VAR Org_ReferenceDate =
EDATE ( [Date], Org_Offset ) -- 6 in this case
VAR UK_Year =
YEAR ( UK_ReferenceDate )
VAR UK_Quarter =
QUARTER ( UK_ReferenceDate )
VAR UK_Month =
MONTH ( UK_ReferenceDate )
VAR UK_Year_Prev = UK_Year - 1
VAR UK_Year_Label =
IF (
UK_StartMonth = 1,
CONVERT ( UK_Year, STRING ),
UK_Year_Prev & "/" & MOD ( UK_Year, 100 )
)
VAR UK_Quarter_Label = UK_Year_Label & " Q" & UK_Quarter
-- Org Financial Year Columns
VAR Org_Year =
YEAR ( Org_ReferenceDate )
VAR Org_Quarter =
QUARTER ( Org_ReferenceDate )
VAR Org_Month =
MONTH ( Org_ReferenceDate )
VAR Org_Year_Prev = Org_Year - 1
VAR Org_Year_Label =
IF (
Org_StartMonth = 1,
CONVERT ( Org_Year, STRING ),
Org_Year_Prev & "/" & MOD ( Org_Year, 100 )
)
VAR Org_Quarter_Label = Org_Year_Label & " Q" & Org_Quarter
RETURN
ROW (
"UK Financial Year", UK_Year,
"UK Financial Quarter", UK_Quarter,
"UK Financial Month", UK_Month,
"UK Financial Year Label", UK_Year_Label,
"UK Financial Quarter Label", UK_Quarter_Label,
"Org Financial Year", Org_Year,
"Org Financial Quarter", Org_Quarter,
"Org Financial Month", Org_Month,
"Org Financial Year Label", Org_Year_Label,
"Org Financial Quarter Label", Org_Quarter_Label
)
)
RETURN
Result
Regards
Thanks Owen,
I think that will get me ther. Really appreciate the support.
I'm new to DAX and not a coder either, resonably competent with an excel formula though. Is there any chance you could offer some further support as you how I integrate this code with my existing code (the Calendar Table I already have), sorry should have posted that first really. Here is it, below. I think it just need to close off the Add Colummn commands and start a new section for this Evaluate command?
Calendar =
ADDCOLUMNS (
CALENDAR ( DATE ( 2015, 1, 1 ), DATE ( 2049, 12, 31 ) ),
"Year", YEAR ( [Date] ),
"Quarter", QUARTER ( [Date] ),
"Month Num", MONTH ( [Date] ),
"Week Num", WEEKNUM ( [Date] ),
"Week Day", WEEKDAY ( [Date] ),
"Day", DAY ( [DATE] ),
"Month", FORMAT ( [Date], "Mmmm" ),
"Day Name", FORMAT ( [Date], "dddd" ),
"Quater Number", "Q" & QUARTER ( [Date] ),
"Year Month",
YEAR ( [Date] ) & " "
& FORMAT ( [Date], "Mmmm" ),
"Year Month Sort", YEAR ( [Date] ) & MONTH ( [Date] ),
"Year Quarter",
YEAR ( [Date] ) & " " & "Q"
& QUARTER ( [Date] ),
"Year Quarter Sort", YEAR ( [Date] ) & QUARTER ( [Date] ),
"1 Month to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -1 )
&& [Date] <= TODAY (),
TRUE ()
),
"3 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -3 )
&& [Date] <= TODAY (),
TRUE ()
),
"6 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -6 )
&& [Date] <= TODAY (),
TRUE ()
),
"12 Months to Date",
IF (
EDATE ( [Date], 0 ) > EDATE ( TODAY (), -12 )
&& [Date] <= TODAY (),
TRUE ()
)
)
Thanks,
Adam
User | Count |
---|---|
53 | |
22 | |
18 | |
18 | |
13 |
User | Count |
---|---|
89 | |
87 | |
50 | |
34 | |
22 |