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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
dpc_development
Helper III
Helper III

Sum rows that have the max date by category

I have a fact table with a date column, a category column, and a numeric column. I want to filter the fact table for only the row that has the max date per category, and then sum up the values in the numeric column.

 

As can be seen in the picture below, the most recent dates are highlighted and the desired total comes up to 152.

 

dpc_development_0-1634492679937.png

 

I started by using SummarizeColumns to find the most recent date per category, but couldn't figure out how to filter the fact table and sum up the number column.

 

MyMeasure =
SUMMARIZECOLUMNS(
    'FACT'[Category],
    "Max Date", MAX('FACT'[Date])
)
1 ACCEPTED SOLUTION
Fowmy
Super User
Super User

@dpc_development 

Add this measure:

 

MaxByCategory = 

SUMX (
    FILTER (
        Fact,
        VAR __V = CALCULATE ( MAX ( Fact[date] ), ALLEXCEPT ( Fact, Fact[category] ) ) RETURN
        Fact[date] = __V 
    ),
    Fact[number]
)

 

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

2 REPLIES 2
Fowmy
Super User
Super User

@dpc_development 

Add this measure:

 

MaxByCategory = 

SUMX (
    FILTER (
        Fact,
        VAR __V = CALCULATE ( MAX ( Fact[date] ), ALLEXCEPT ( Fact, Fact[category] ) ) RETURN
        Fact[date] = __V 
    ),
    Fact[number]
)

 

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

dpc_development
Helper III
Helper III

I was able to get a solution like this, but not sure whether it is the best DAX code for this.

Remaining Stock = 
SUMX(
    SUMMARIZE(
        'FACT',
        [Category],
        "Max Date", MAX('FACT'[Date])
    ),
    LOOKUPVALUE('FACT'[NumericColumn],'FACT'[Category], [Category],'FACT'[Date], [Max Date])
)

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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