Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi All,
Im trying to obtain rolling Average with the measure for below sample data:
Date | Month | Year | Country | Value |
01-06-2021 | June | 2021 | abc | 0 |
01-07-2021 | July | 2021 | abc | 77.16 |
01-05-2024 | May | 2024 | abc | 213.36 |
01-07-2024 | July | 2024 | abc | 189.39 |
01-10-2024 | October | 2024 | abc | 189.39 |
01-11-2024 | November | 2024 | abc | 189.39 |
01-01-2021 | January | 2021 | abc | 0 |
01-02-2021 | February | 2021 | abc | 0 |
01-03-2021 | March | 2021 | abc | 0 |
01-04-2021 | April | 2021 | abc | 0 |
01-05-2021 | May | 2021 | abc | 0 |
01-08-2021 | August | 2021 | abc | 0 |
01-09-2021 | September | 2021 | abc | 0 |
01-10-2021 | October | 2021 | abc | 0 |
01-11-2021 | November | 2021 | abc | 0 |
01-12-2021 | December | 2021 | abc | 0 |
01-01-2022 | January | 2022 | abc | 0 |
01-02-2022 | February | 2022 | abc | 0 |
01-03-2022 | March | 2022 | abc | 0 |
01-04-2022 | April | 2022 | abc | 0 |
01-05-2022 | May | 2022 | abc | 0 |
01-06-2022 | June | 2022 | abc | 0 |
01-07-2022 | July | 2022 | abc | 0 |
01-08-2022 | August | 2022 | abc | 0 |
01-09-2022 | September | 2022 | abc | 0 |
01-10-2022 | October | 2022 | abc | 0 |
01-11-2022 | November | 2022 | abc | 0 |
01-12-2022 | December | 2022 | abc | 0 |
01-01-2023 | January | 2023 | abc | 0 |
01-02-2023 | February | 2023 | abc | 0 |
01-03-2023 | March | 2023 | abc | 0 |
01-04-2023 | April | 2023 | abc | 0 |
01-05-2023 | May | 2023 | abc | 0 |
01-06-2023 | June | 2023 | abc | 0 |
01-07-2023 | July | 2023 | abc | 0 |
01-08-2023 | August | 2023 | abc | 0 |
01-09-2023 | September | 2023 | abc | 0 |
01-10-2023 | October | 2023 | abc | 0 |
01-11-2023 | November | 2023 | abc | 0 |
01-12-2023 | December | 2023 | abc | 0 |
01-01-2024 | January | 2024 | abc | 0 |
01-02-2024 | February | 2024 | abc | 0 |
01-03-2024 | March | 2024 | abc | 0 |
01-04-2024 | April | 2024 | abc | 0 |
01-06-2024 | June | 2024 | abc | 0 |
01-08-2024 | August | 2024 | abc | 0 |
01-09-2024 | September | 2024 | abc | 0 |
Dax im using :
Solved! Go to Solution.
Hi @hungry_learner ,
The issue you're facing lies in this part of your DAX formula:
SELECTEDVALUE('3-Year Data'[Country])
This function works perfectly when a single value is selected in the slicer for Country, but if no selection is made (or multiple values are selected), SELECTEDVALUE returns BLANK. This is why your line chart doesn't display any data when no country is selected—it is expecting a single country to be selected to calculate the rolling average.
The SELECTEDVALUE function only returns a value when exactly one item is selected in the slicer. If:
In your case, the rolling average calculation depends on a specific country being selected. When no selection is made, the formula fails to evaluate because SELECTEDVALUE is returning BLANK.
you can default to calculating the rolling average across all countries when no country is selected.
CALCULATE(
AVERAGEX(
FILTER(
ALLSELECTED('3-Year Data'),
(ISBLANK(SELECTEDVALUE('3-Year Data'[Country])) ||
'3-Year Data'[Country] = SELECTEDVALUE('3-Year Data'[Country])) &&
'3-Year Data'[Date] <= MAX('3-Year Data'[Date]) &&
'3-Year Data'[Date] > EDATE(MAX('3-Year Data'[Date]), -36)
),
'3-Year Data'[Value]
)
)
Hi,
You should first of all create a Calendar Table with calculated column formulas for Year, Month name and Month number. Sort the Month name by the Month number. Create a relationship (Many to One and single) from the Date column of your Fact table to the Date column of the Calendar Table. To your visual/slicers/filters, add Year, Month from the Calendar Table. Modify your measure to:
Total = SUM('3-Year Data'[Value])
Measure = AVERAGEX(DATESBETWEEN(Calendar[date],MAX(Calendar[Date]),EDATE(MAX('3-Year Data'[Date]), -36)),[Total])
Hope this helps.
Rolling Average 36M =
CALCULATE(
AVERAGEX(
FILTER(
ALL('3-Year Data'),
'3-Year Data'[Country] = SELECTEDVALUE('3-Year Data'[Country]) &&
'3-Year Data'[Date] <= MAX('3-Year Data'[Date]) &&
'3-Year Data'[Date] > EDATE(MAX('3-Year Data'[Date]), -36)
),
'3-Year Data'[Value]
)
)
Ensure that the Date column in '3-Year Data' is properly formatted as a date.
Use '3-Year Data'[Date] as the X-axis to plot the rolling average over time.
Hi @hungry_learner ,
The issue you're facing lies in this part of your DAX formula:
SELECTEDVALUE('3-Year Data'[Country])
This function works perfectly when a single value is selected in the slicer for Country, but if no selection is made (or multiple values are selected), SELECTEDVALUE returns BLANK. This is why your line chart doesn't display any data when no country is selected—it is expecting a single country to be selected to calculate the rolling average.
The SELECTEDVALUE function only returns a value when exactly one item is selected in the slicer. If:
In your case, the rolling average calculation depends on a specific country being selected. When no selection is made, the formula fails to evaluate because SELECTEDVALUE is returning BLANK.
you can default to calculating the rolling average across all countries when no country is selected.
CALCULATE(
AVERAGEX(
FILTER(
ALLSELECTED('3-Year Data'),
(ISBLANK(SELECTEDVALUE('3-Year Data'[Country])) ||
'3-Year Data'[Country] = SELECTEDVALUE('3-Year Data'[Country])) &&
'3-Year Data'[Date] <= MAX('3-Year Data'[Date]) &&
'3-Year Data'[Date] > EDATE(MAX('3-Year Data'[Date]), -36)
),
'3-Year Data'[Value]
)
)
Hi @hungry_learner ,
There is nothing wrong with the expression, in your logic you need to specify country by SELECTEDVALUE and then calculate the average over three years. That means it doesn't work when you don't select country. Perhaps you need to use HASONEFILTER(). Also, you need to calculate another part of the logic that needs to be displayed when unselected.
HASONEFILTER function (DAX) - DAX | Microsoft Learn
Hope it helps!
Best regards,
Community Support Team_ Scott Chang
If this post helps then please consider Accept it as the solution to help the other members find it more quickly.
User | Count |
---|---|
119 | |
78 | |
58 | |
52 | |
46 |
User | Count |
---|---|
170 | |
117 | |
63 | |
58 | |
51 |