Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
Hello Team,
I have a dim_calendar and fact table. I have used slicers for columns Year(2024) single select & YYYYMM (202401) multi select.
I have YYYYMM column in fact table which is related to dim_calendar. I need to calculate sum of Sales for LY. If I chose 202401, then sales for 202301 should be returned and if there are multi selections then sum of respective months should be displayed. How to achieve this ? If no selection is made then sum of sales for selected year should be displayed. I have also created YYYYMM-1 (202301) & YYYYMM-2 (202201) columns in Dim_calendar. Immediate response would be much appreciated. Thanks.
Solved! Go to Solution.
Remove or inactivate the YYYYMM relationship and activate the date relationship. Use standard DAX.
@varung8899 Default to Sum of Sales for Selected Year if No Selection: If no YYYYMM is selected, the measure should default to summing the sales for the entire selected year.
DAX
LY Sales =
VAR SelectedYearMonths = VALUES(dim_calendar[YYYYMM])
VAR SelectedYear = SELECTEDVALUE(dim_calendar[Year])
VAR LYYearMonths =
SELECTCOLUMNS(
FILTER(
dim_calendar,
dim_calendar[YYYYMM] IN SelectedYearMonths
),
"LYYYYYMM", dim_calendar[YYYYMM-1]
)
RETURN
IF(
ISBLANK(SelectedYearMonths),
CALCULATE(
SUM(fact_table[Sales]),
dim_calendar[Year] = SelectedYear - 1
),
CALCULATE(
SUM(fact_table[Sales]),
dim_calendar[YYYYMM] IN LYYearMonths
)
)
This DAX measure will dynamically calculate the sum of sales for the previous year based on the selected YYYYMM values. If no YYYYMM is selected, it will default to summing the sales for the entire previous year.
Proud to be a Super User! |
|
Thank you. But I receive error stating a table of multiple values was supplied where a single value was expected. Pls advise.
@varung8899 , This must be due to Selected value
LY Sales =
VAR SelectedYearMonths = VALUES(dim_calendar[YYYYMM])
VAR SelectedYear = SELECTEDVALUE(dim_calendar[Year])
VAR LYYearMonths =
SELECTCOLUMNS(
FILTER(
dim_calendar,
dim_calendar[YYYYMM] IN SelectedYearMonths
),
"LYYYYYMM", dim_calendar[YYYYMM-1]
)
RETURN
IF(
ISBLANK(SelectedYearMonths),
CALCULATE(
SUM(fact_table[Sales]),
dim_calendar[Year] = SelectedYear - 1
),
CALCULATE(
SUM(fact_table[Sales]),
dim_calendar[YYYYMM] IN LYYearMonths
)
)
Proud to be a Super User! |
|
This is the same formula as before and it does not work unfortunately. @lbendlin Could you please provide your expert advise ?
A calendar dimension table must be based on dates. Your fact table needs to be modified to contain a date field ( first day of month, for example). Once you join the tables on the date you can use regular time intelligence functions like SAMEPERIODLASTYEAR.
@lbendlin I have created date column in Fact table now and related it to Dim_calendar. The relation appears as inactive (dotted line) since I already have YYYYMM columns in relationship. Could you please help me frame the DAX ? I am particularly interested for value when single & multiple values are selected in YYYYMM slicer.
Remove or inactivate the YYYYMM relationship and activate the date relationship. Use standard DAX.
Thank you @lbendlin . Found the solution using below code. Please help to find LY-1 Sales if you know since I don't see a direct function available for that.
Remember that SAMEPERIODLASTYEAR is syntax sugar for DATEADD(x,-12,MONTH). So for LY-1 use SAMEPERIODLASTYEAR(SAMEPERIODLASTYEAR)) or simpler DATEADD(x,-24,MONTH)
Also please note that YYYYMM & related LY columns are in Number format. Below is sample data when I select 2024 and 202401 in slicers. Sum column is from fact table.
User | Count |
---|---|
16 | |
14 | |
13 | |
12 | |
11 |
User | Count |
---|---|
19 | |
16 | |
15 | |
11 | |
9 |