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

Learn from the best! Meet the four finalists headed to the FINALS of the Power BI Dataviz World Championships! Register now

Reply
alee5210
Helper II
Helper II

Count/Sum Latest Record Per ID Based On Multiple Date Conditions

Hi All, I'm stuck with a tricky question. I have some data and I want to do the count or the sum based on multiple conditions that are given. The difficult aspect is that there are multiple date fields that all need to be filtered on and I only want to count or sum using the latest date for each unique ID.

 

First I have this reporting table, this is only showing June but it goes back many months. In the dashboard there are 2 single select slicer that the users MUST select

1. reporting_date which let's users look back at how things were in previous months

2. data_period which let's them see the past 1 year, 3 months, 1 month etc

reporting_datedata_periodstart_calendar_dateend_calendar_date
01-June-24From beginning01-Jan-7001-June-24
01-June-24Last 1 year01-June-2301-June-24
01-June-24Last 3 months01-Feb-2401-June-24
01-June-24Last 1 month01-May-2401-June-24

 

My data looks like the following. The data works as a slow changing dimension table. So a unique ID's transit start date will never change, but if the mass of the item is remeasured throughout the journey, the eff_from (effective from) and eff_to (effective to) can be updated in our records.

Unique_IDTransit_startMasseff_fromeff_to
aaa01-Apr-24401-Apr-2413-Apr-24
bbb04-Apr-241504-Apr-2416-Apr-24
ccc28-Apr-241228-Apr-2410-May-24
ddd01-May-24301-May-2413-May-24
aaa01-Apr-24914-Apr-2431-Dec-99
bbb04-Apr-24217-Apr-2431-Dec-99
ccc28-Apr-24711-May-2431-Dec-99
ddd01-May-24414-May-2431-Dec-99

 

The rules to calculate the count/sum are:

For each unique ID, only use the latest record/row that meets the following conditions

1. the eff_from <= reporting_date

2. the eff_to > reporting date

3. start_calendar_date <= test_start_date 

4. end_calendar_date >= test_start_date

 

I would like this to be put into bar charts/line charts so maybe a Calculation Group would be effective here but I have not seen many examples on how to apply it to such a measure. Any help would be greatly appreciated.

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @alee5210 

I would suggest using INDEX to filter on the latest record per Unique ID. I take it that "latest" is based on the eff_from column.

 

(Sample PBIX attached)

 

Here is an example measure Mass Sum Latest, including the conditions you listed, assuming you already have Mass Sum = SUM ( Data[Mass] ):

Mass Sum Latest = 
VAR ReportingDate =
    SELECTEDVALUE ( Reporting[reporting_date] )
VAR StartCalendarDate =
    SELECTEDVALUE ( Reporting[start_calendar_date] )
VAR EndCalendarDate =
    SELECTEDVALUE ( Reporting[end_calendar_date] )
VAR SourceTable =
    CALCULATETABLE (
        SUMMARIZE ( Data, Data[Unique_ID], Data[eff_from] ),
        -- Use KEEPFILTERS to retain any existing filters on these columns
        KEEPFILTERS ( Data[eff_from] <= ReportingDate ),
        KEEPFILTERS ( Data[eff_to] > ReportingDate ),
        KEEPFILTERS ( StartCalendarDate <= Data[Transit_start] ),
        KEEPFILTERS ( EndCalendarDate >= Data[Transit_start] )
    )
VAR LatestRecords =
    INDEX (
        1,
        SourceTable,
        ORDERBY ( Data[eff_from], DESC ),
        DEFAULT,
        PARTITIONBY ( Data[Unique_ID] )
    )
VAR Result =
    CALCULATE ( [Mass Sum], LatestRecords )
RETURN
    Result

You can apply this logic to any other measure by replacing [Mass Sum] with any other measure, or SELECTEDMEASURE () in a calculation item. There is a calculation group with a single calculation item in the attached PBIX.

OwenAuger_0-1723363742782.png

 

Is this the sort of thing you were looking for? Let me know if any of the logic is wrong.

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @alee5210 

I would suggest using INDEX to filter on the latest record per Unique ID. I take it that "latest" is based on the eff_from column.

 

(Sample PBIX attached)

 

Here is an example measure Mass Sum Latest, including the conditions you listed, assuming you already have Mass Sum = SUM ( Data[Mass] ):

Mass Sum Latest = 
VAR ReportingDate =
    SELECTEDVALUE ( Reporting[reporting_date] )
VAR StartCalendarDate =
    SELECTEDVALUE ( Reporting[start_calendar_date] )
VAR EndCalendarDate =
    SELECTEDVALUE ( Reporting[end_calendar_date] )
VAR SourceTable =
    CALCULATETABLE (
        SUMMARIZE ( Data, Data[Unique_ID], Data[eff_from] ),
        -- Use KEEPFILTERS to retain any existing filters on these columns
        KEEPFILTERS ( Data[eff_from] <= ReportingDate ),
        KEEPFILTERS ( Data[eff_to] > ReportingDate ),
        KEEPFILTERS ( StartCalendarDate <= Data[Transit_start] ),
        KEEPFILTERS ( EndCalendarDate >= Data[Transit_start] )
    )
VAR LatestRecords =
    INDEX (
        1,
        SourceTable,
        ORDERBY ( Data[eff_from], DESC ),
        DEFAULT,
        PARTITIONBY ( Data[Unique_ID] )
    )
VAR Result =
    CALCULATE ( [Mass Sum], LatestRecords )
RETURN
    Result

You can apply this logic to any other measure by replacing [Mass Sum] with any other measure, or SELECTEDMEASURE () in a calculation item. There is a calculation group with a single calculation item in the attached PBIX.

OwenAuger_0-1723363742782.png

 

Is this the sort of thing you were looking for? Let me know if any of the logic is wrong.

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Hi Owen, this worked really well! Exactly what I was after. Thanks so much!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

February Power BI Update Carousel

Power BI Monthly Update - February 2026

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