Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
JaysYee
Frequent Visitor

Identify Most Recent Count

I'm trying to find the most recent distinct count value from a text string grouping using DAX.

For example, here is the underlying data:

Year-QuarterEmployee Name
2023-Q1A
2023-Q1B
2023-Q1C
2023-Q1D
2023-Q1E
2023-Q2A
2023-Q2B
2023-Q2C
2023-Q2D
2023-Q3A
2023-Q3B
2023-Q3C
2023-Q3D

 

I would ultimately like a card that displays the count of the most recent Year-Quarter, but when applying a filter on a specific Year-Quarter the card will show that quarter's count.  For example:

Year-QuarterDistinct Employee Count
2023-Q15
2023-Q24
2023-Q34

 

By default (or all) the card will show 4, but when I select the 2023-Q1 filter the card will show 5.

 

I've tried:

  1. Creating a column rankx on the Year-Quarter
  2. Then creating a measure where by I used "group by" to group the Year-Quarter and index (from step1) and then tried to return the last value of the grouped table.  Something like:

 

Recent Count =
var CountTable = GROUPBY('Employee Count',Employee Count[Year-Quarter],Employee Count[Rank],"Quarter Count",COUNTROWS(CURRENTGROUP()))
return
    LASTONBLANK(?)

 

I just can't figure out how to find the last value of the most recent Year-Quarter in a table created by the "group by" function.

I searched past posts, and I can't find a solution.

 

Thank you!

3 REPLIES 3
v-yangliu-msft
Community Support
Community Support

Hi  @JaysYee ,

 

Here are the steps you can follow:

1 . In Power Query -- Add Column – Index Column – From 1.

vyangliumsft_0-1676516697017.png

2. Create measure.

Measure =
var _selectYearQu=
SELECTEDVALUE('Employee Count'[Year-Quarter])
var _Category=
SELECTEDVALUE('Employee Count'[Category])
var _max=
MAXX(FILTER(ALL('Employee Count'),'Employee Count'[Index]=MAXX(ALLSELECTED('Employee Count'),[Index])),'Employee Count'[Year-Quarter])
return
IF(
    HASONEVALUE('Employee Count'[Year-Quarter]),
    CALCULATE(DISTINCTCOUNT('Employee Count'[Employee Name]),ALLSELECTED('Employee Count'))
    ,
    CALCULATE(DISTINCTCOUNT('Employee Count'[Employee Name]),FILTER(ALL('Employee Count'),
    'Employee Count'[Year-Quarter]=_max))
)

3. Result:

vyangliumsft_1-1676516697018.png

vyangliumsft_2-1676516697019.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

v-yangliu-msft
Community Support
Community Support

Hi  @JaysYee ,

 

Here are the steps you can follow:

1. In Power Query -- Add Column – Index Column – From 1.

vyangliumsft_0-1675926882768.png

2. Create measure.

Measure =
var _select=SELECTCOLUMNS('Employee Count',"select",[Year-Quarter])
var _max=
MAXX(FILTER(ALL('Employee Count'),'Employee Count'[Index]=MAXX(ALLSELECTED('Employee Count'),[Index])),[Year-Quarter])
return
IF(
    NOT(HASONEVALUE('Employee Count'[Year-Quarter])) ,   
    CALCULATE(DISTINCTCOUNT('Employee Count'[Employee Name]),FILTER(ALL('Employee Count'),'Employee Count'[Year-Quarter]=_max))
    ,
    CALCULATE(DISTINCTCOUNT('Employee Count'[Employee Name]),FILTER(ALL('Employee Count'),'Employee Count'[Year-Quarter]=_select))
)

3. Result:

By default (or all):

vyangliumsft_1-1675926882768.png

Select 2023-Q3:

vyangliumsft_2-1675926882770.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hi @v-yangliu-msft ,

 

Thank you that worked.  I'm sorry I should have included additional columns, like the category of employees.  When applying an additional filter then the card doesn't seem to reflect it.

For example:

Year-QuarterEmployee NameCategory
2023-Q1ARemote
2023-Q1BRemote
2023-Q1COffice
2023-Q1DOffice
2023-Q1EOffice
2023-Q2ARemote
2023-Q2BRemote
2023-Q2COffice
2023-Q2DOffice
2023-Q3ARemote
2023-Q3BRemote
2023-Q3COffice
2023-Q3DOffice

 

So 2023-Q1 should show 3 when applying Office filter while 2023-Q4 should show 2.

 

Is there a way to make it more dynamic so it will accept other filters/columns?

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.