Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
The following is a sample user dataset where each user is assigned a rating at random times during the year (Some users are missing ratings for some of the years).
I have a matrix visual with the average rating per user, per year. (See example below). So John Smith had 2 enteries for the year 2020 and the matrix has a single average value for that user, for each year that they have data for.
What I'm trying to achieve is the Diff coloum (last coloum in 2nd visual), which is the difference between the 'latest average yearly rating' and 'earliest average yearly rating'. In Johns case this will be 1.225-0.936 (his value in 2021-2019). So "the earliest year" and "the latest year" with data could be different for each user.
How can I calculate this coloum/ measure with DAX?
Any help is much appreciated.
Solved! Go to Solution.
@Anonymous to be able to achieve this, you would need a Calendar Table. Once you have that, create a relationship between Fact[Date] and Calendar[Calendar_Date] which will look like below
Then you can write the following meaure to achieve what you need
Measure =
VAR _mxYear =
CALCULATE (
CALCULATE (
MAX ( 'Calendar'[Calendar_Year] ),
ALLEXCEPT ( 'Fact', 'Fact'[ID] )
),
CROSSFILTER ( 'Fact'[Date], 'Calendar'[Calendar_Date], BOTH )
)
VAR _minYear =
CALCULATE (
CALCULATE (
MIN ( 'Calendar'[Calendar_Year] ),
ALLEXCEPT ( 'Fact', 'Fact'[ID] )
),
CROSSFILTER ( 'Fact'[Date], 'Calendar'[Calendar_Date], BOTH )
)
VAR _minAvg =
CALCULATE (
AVERAGE ( 'Fact'[Rating] ),
FILTER (
VALUES ( 'Calendar'[Calendar_Year] ),
'Calendar'[Calendar_Year] = _minYear
)
)
VAR _maxAvg =
CALCULATE (
AVERAGE ( 'Fact'[Rating] ),
FILTER (
VALUES ( 'Calendar'[Calendar_Year] ),
'Calendar'[Calendar_Year] = _mxYear
)
)
RETURN
_maxAvg - _minAvg
From
to
@Anonymous to be able to achieve this, you would need a Calendar Table. Once you have that, create a relationship between Fact[Date] and Calendar[Calendar_Date] which will look like below
Then you can write the following meaure to achieve what you need
Measure =
VAR _mxYear =
CALCULATE (
CALCULATE (
MAX ( 'Calendar'[Calendar_Year] ),
ALLEXCEPT ( 'Fact', 'Fact'[ID] )
),
CROSSFILTER ( 'Fact'[Date], 'Calendar'[Calendar_Date], BOTH )
)
VAR _minYear =
CALCULATE (
CALCULATE (
MIN ( 'Calendar'[Calendar_Year] ),
ALLEXCEPT ( 'Fact', 'Fact'[ID] )
),
CROSSFILTER ( 'Fact'[Date], 'Calendar'[Calendar_Date], BOTH )
)
VAR _minAvg =
CALCULATE (
AVERAGE ( 'Fact'[Rating] ),
FILTER (
VALUES ( 'Calendar'[Calendar_Year] ),
'Calendar'[Calendar_Year] = _minYear
)
)
VAR _maxAvg =
CALCULATE (
AVERAGE ( 'Fact'[Rating] ),
FILTER (
VALUES ( 'Calendar'[Calendar_Year] ),
'Calendar'[Calendar_Year] = _mxYear
)
)
RETURN
_maxAvg - _minAvg
From
to
@Anonymous Hi!
Could you paste as a comment the table on which to calculate the required measurement?
Thx,
B.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
14 | |
11 | |
10 | |
10 | |
9 |
User | Count |
---|---|
18 | |
13 | |
12 | |
11 | |
8 |