Don't miss your chance to take the Fabric Data Engineer (DP-600) exam for FREE! Find out how by attending the DP-600 session on April 23rd (pacific time), live or on-demand.
Learn moreNext up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now
I have two measures. The first one calculates sales from the year selected in a slicer:
Sales_this_year =
VAR V_year = SELECTEDVALUE(my_table[YEAR])
RETURN
CALCULATE(SUM(my_table[SALES]),
my_table[YEAR] = V_year)
The second one calculates sales from the previous year; the only change to the DAX is subtracting 1 from the year selected in the slicer:
Sales_last_year =
VAR V_year = SELECTEDVALUE(my_table[YEAR])-1
RETURN
CALCULATE(SUM(my_table[SALES]),
my_table[YEAR] = V_year)
The two measures are then displayed on my report in separate cards.
Having two measures that are so similar seems inefficient, especially when scaled up to a full report with many measures like this showing this year's and last year's results.
Is it possible to create a single measure like this, and display it in both cards with filters on each card to determine the year each should be looking at?
Sales = SUM(my_table[SALES])
I'm also open to different ideas if anyone can think of a better way.
Can you try the following ?
Sales = CALCULATE(SUM(my_table[SALES]), ALL(my_table), FILTER(my_table, my_table[YEAR] = SELECTEDVALUE(my_table[YEAR])))
The measure uses the CALCULATE function to calculate the sum of sales, but it also uses the ALL function to remove any filters that are applied to the table. This means that the measure will calculate the sum of sales for all years, regardless of which year is selected in the slicer.
For example, to create a card that shows sales for this year, you would use the following formula:
Sales This Year = CALCULATE(SUM(my_table[SALES]), ALL(my_table), FILTER(my_table, my_table[YEAR] = SELECTEDVALUE(my_table[YEAR])))
Another approach would be to use a parameter to determine the year to calculate sales for. This would allow you to create a single measure that can be used to calculate sales for any year, without having to change the formula.
To do this, you would first create a parameter for the year. Then, you would use the CALCULATE function with the FILTER function to calculate sales for the selected year.
Sales = CALCULATE(SUM(my_table[SALES]), FILTER(my_table, my_table[YEAR] = SELECTEDVALUE(parameter_year)))
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.
Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.
| User | Count |
|---|---|
| 43 | |
| 43 | |
| 38 | |
| 18 | |
| 15 |
| User | Count |
|---|---|
| 67 | |
| 63 | |
| 30 | |
| 30 | |
| 23 |