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,
Could anybody help me with my task, please.
I am using a DirectQuery mode.
My Table 1 contains columns:
- activity_type
- client_id
- activity_id
- activity_date
- score
I have a reporting date, the last date of a month, that we choose using a slicer.
I need to find for each client_id, activity_type the last activity_id and corresponding score such that activity_date<=reporting_date and display that in Table 2.
After that we need to count for each activity_type the number of clients for which score is not null and find the average score.
Would be greatful for any hints
Many thanks!
Solved! Go to Solution.
Hi @marypal ,
Please try code as below to create measures.
Avg Score =
VAR _ReportDate =
SELECTEDVALUE ( 'Calendar'[Date] )
VAR _GENERATE =
GENERATE (
VALUES ( 'Table 1'[activity_type] ),
VALUES ( 'Table 1'[client_id] )
)
VAR _ADD1 =
ADDCOLUMNS (
_GENERATE,
"Score",
VAR _MAXDATE =
CALCULATE (
MAX ( 'Table 1'[activity_date] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] <= _ReportDate
)
)
RETURN
CALCULATE (
SUM ( 'Table 1'[score] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] = _MAXDATE
)
)
)
RETURN
AVERAGEX (
FILTER ( _ADD1, [activity_type] = MAX ( 'Table 1'[activity_type] ) ),
[Score]
)
Number of Clients =
VAR _ReportDate =
SELECTEDVALUE ( 'Calendar'[Date] )
VAR _GENERATE =
GENERATE (
VALUES ( 'Table 1'[activity_type] ),
VALUES ( 'Table 1'[client_id] )
)
VAR _ADD1 =
ADDCOLUMNS (
_GENERATE,
"Score",
VAR _MAXDATE =
CALCULATE (
MAX ( 'Table 1'[activity_date] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] <= _ReportDate
)
)
RETURN
CALCULATE (
SUM ( 'Table 1'[score] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] = _MAXDATE
)
)
)
RETURN
COUNTX(FILTER(_ADD1,[activity_type] = MAX('Table 1'[activity_type]) && [Score]<>BLANK()),[activity_type])
Result is as below.
Best Regards,
Rico Zhou
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @marypal ,
Please try code as below to create measures.
Avg Score =
VAR _ReportDate =
SELECTEDVALUE ( 'Calendar'[Date] )
VAR _GENERATE =
GENERATE (
VALUES ( 'Table 1'[activity_type] ),
VALUES ( 'Table 1'[client_id] )
)
VAR _ADD1 =
ADDCOLUMNS (
_GENERATE,
"Score",
VAR _MAXDATE =
CALCULATE (
MAX ( 'Table 1'[activity_date] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] <= _ReportDate
)
)
RETURN
CALCULATE (
SUM ( 'Table 1'[score] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] = _MAXDATE
)
)
)
RETURN
AVERAGEX (
FILTER ( _ADD1, [activity_type] = MAX ( 'Table 1'[activity_type] ) ),
[Score]
)
Number of Clients =
VAR _ReportDate =
SELECTEDVALUE ( 'Calendar'[Date] )
VAR _GENERATE =
GENERATE (
VALUES ( 'Table 1'[activity_type] ),
VALUES ( 'Table 1'[client_id] )
)
VAR _ADD1 =
ADDCOLUMNS (
_GENERATE,
"Score",
VAR _MAXDATE =
CALCULATE (
MAX ( 'Table 1'[activity_date] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] <= _ReportDate
)
)
RETURN
CALCULATE (
SUM ( 'Table 1'[score] ),
FILTER (
'Table 1',
'Table 1'[activity_type] = EARLIER ( [activity_type] )
&& 'Table 1'[client_id] = EARLIER ( [client_id] )
&& 'Table 1'[activity_date] = _MAXDATE
)
)
)
RETURN
COUNTX(FILTER(_ADD1,[activity_type] = MAX('Table 1'[activity_type]) && [Score]<>BLANK()),[activity_type])
Result is as below.
Best Regards,
Rico Zhou
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thank you very much, Rico!
I need to find for each client_id, activity_type the last activity_id and corresponding score
The max([activity _id]) is not a problem. However the "corresponding score" is, as you cannot easily aggregate that.
You will likely need to use table variables in your measure and filter that table variable. Not sure how feasible that is in Direct Query mode.
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 |
---|---|
19 | |
14 | |
14 | |
11 | |
9 |