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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
alexbjorlig
Helper IV
Helper IV

How to group table by date

I'm new to DAX, and need to find the latest value by date in a measure.

 

Input:

 

category    datevalue1value2
A3/3/2020 04:30:0010
A2/1/2020 04:30:0001
A1/1/2020 04:30:0001
B1/1/2020 04:30:0011
B2/2/2020 06:30:0000
C1/1/2020 04:30:0010

 

Desired output:

 

category    datevalue1value2
A3/3/2020 04:30:0010
B2/2/2020 06:30:0000
C1/1/2020 04:30:0010

 

What I have tried:

 

 

 

 

 

GROUPBY(Table, Table[category], "Latest", MAXX(CURRENTGROUP(), Table[date]))

 

 

 

 

 

But this "only" gives me:

 

category     date
A3/3/2020 04:30:00
B2/2/2020 06:30:00
C1/1/2020 04:30:00

 

I feel like I should be able to use a filter function, but did not succeed in making it work 🙃

Thanks in advance for any help 😅

1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@alexbjorlig 

Try this table, it will work for any number of columns in your table

 

New Table  = 

FILTER(
    table,
    VAR __cat = Table[category    ] RETURN 
    Table[date] = CALCULATE( MAX(Table[date]) , REMOVEFILTERS() , Table[category    ] = __cat)

)

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

View solution in original post

7 REPLIES 7
Jihwan_Kim
Super User
Super User

Hi,

If you want to use groupby function, please try the below.

 

Picture2.png

 

New Table =
VAR newtable =
GROUPBY (
'Table',
'Table'[category],
"Latest Date", MAXX ( CURRENTGROUP (), 'Table'[date] )
)
VAR newtableconnect =
CALCULATETABLE (
'Table',
TREATAS ( newtable, 'Table'[category], 'Table'[date] )
)
RETURN
newtableconnect

 

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

Whaaat - that is also working and another interesting solution. Would there be any differnce to the results compared to @Fowmy - or is more of a prefrence thing?

 

I'm already using GroupBy to a bunch of my measures, so I kind of like the idea, but comparing the number of lines written, the other solution is more compact.

Fowmy
Super User
Super User

@alexbjorlig 

Try this table, it will work for any number of columns in your table

 

New Table  = 

FILTER(
    table,
    VAR __cat = Table[category    ] RETURN 
    Table[date] = CALCULATE( MAX(Table[date]) , REMOVEFILTERS() , Table[category    ] = __cat)

)

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

I know it'sprobably super hard to explain, but why does this work - I'm so impressed! 😂

 

The CALCULATE uses the REMOVEFILTERS() --> but I guess that could be avoided, or what does that exactly do? 

And the __cat variable is a refrence to the filter context, making sure it's only done for the "correct" category?

 

So simple, so elegant 😎

@alexbjorlig 

With the Filter function, there is a row context, you only one record at a time, REMOVEFILTERS clears the row context filter and shows all the records and again _cat filters by category obtained from the row context. Now the max date is taken and matched. 

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Amazing.

So they key difference to understand for me here, is the "filter" context vs row context. The REMOVEFILTERS only clears the row context, so any filters there would still "work"?

amitchandak
Super User
Super User

@alexbjorlig , Based on what I got. In a table visual Take Max of value 1 and Min of value 2

 

Or create a new table

summarize(Table, Table[Category], Table[Date], "Val1", Max(Table[Value1]), "Val2", Min(Table[Value2]))

Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors