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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Measure that will return count of ids for each value based on max date in history table

I am trying to create a measure that will count the number of ids that held a certain value prior to a selected day. Rows in my fact table are only updated when the value changes so I need the user to be able to select a day and get a view of the value as of the most recent update.

 

So far this is what I have:

 

I have a history/fact table ('historyTable') and a disconnected date table ('dateTable') as follows:

 

 'dateTable' (disconnected):

 

date
11/14/2019
11/13/2019
...
3/21/2015
1/1/2015

 

  'historyTable':

 

 Id  

dimension  dimSelected dimDate
ATeamGreen7/10/2019
ATeamBlue4/14/2019
ATeamRed10/29/2018
BTeamPurple9/23/2019
BTeamRed1/3/2019
BTeamBlack10/12/2018
ARepMark7/10/2019
ARepJoe4/14/2019

A

Rep

Max

10/29/2018

B

Rep

Sally

9/23/2019

B

Rep

Sue

1/3/2019

B

Rep

Bob

10/12/2018

 

The slicer on the 'dimension' is set up so that a user selection is always required, so for this example let's imagine the user has selected 'Team', resulting in the historyTable data filtered down to only the 'Team' dimension

 

'historyTable' (filtered by dimension = Team):

 

 Id  

dimension  dimSelected dimDate
ATeamGreen7/10/2019
ATeamBlue4/14/2019
ATeamRed10/29/2018
BTeamPurple9/23/2019
BTeamRed1/3/2019
BTeamBlack10/12/2018

 

I am trying to create a measure that can be dropped into a table with 'historyTable'[dimSelected] that will count the distinct number of Ids with the dimSelected value based on the max date

 

So with 11/14/2019 selected from dateTable[date] and 'Team' selected from the dimension filter the following counts should be returned

 

'historyTable'[dimSelected] [idCount]
Green1
Blue0
Red0
Purple1
Black0

 

But when the user changes the date selection to 6/30/2019 for example, the measure should recalculate to return the following counts

 

'historyTable'[dimSelected] [idCount]
Green0
Blue1
Red1
Purple0
Black0


So far I have the following measure that based on the date selected seems to calculate the max dimSelected value correctly when dropped into a table with historyTable[id] as the row context

 

[lastValue] =
   VAR viewDate = SELECTEDVALUE(dateTable[date])
   VAR maxUpdate = CALCULATE(MAX(historyTable[dimDate]), FILTER(historyTable, historyTable[dimDate[ <= viewDate))
   VAR maxValue = CALCULATE(LASTNONBLANK(historyTable[dimDate], [dimDate]), historyTable[dimDate]<=maxUpdate)
RETURN
   MAXX(FILTER(ALLSELECTED(historyTable), 'historyTable'[dimDate] = maxValue), 'historyTable'[dimSelected])

 

The above measure provides the following when dropped into a table with id values which I believe is the expected output as far as calculating the most up-to-date value based on date selected

 

'historyTable'[id] [lastValue]
AGreen
BPurple

 

When the date selection changes to 6/30/2019 the measure recalculates and returns the following

 

'historyTable'[id] [lastValue]
ABlue
BRed


I am having trouble with the next step that will translate [lastValue] to a measure that will calculate counts for dimSelected.

I have tried the following but when I drop it into a table with 'historyTable'[dimSelected] values I get a count for each value that the id ever held rather than a count for the max value based on date selected

 

[idCount] =
   VAR maxValue = [lastValue]
RETURN
   CALCULATE(COUNTROWS(VALUES('historyTable'[Id])),

                       FILTER(ALLSELECTED(historyTable), 'historyTable'[dimSelected] = maxValue)

 

This measure currently returns the following in a table with dimSelected values and 11/14/2019 selected from dateTable[date]:

 

'historyTable'[dimSelected] [idCount]
Green1
Blue1
Red2
Purple1
Black1

 

Is there a way to adjust my [idCount] measure so it only returns the count for the max value for each id according to the date selected?

 

1 REPLY 1
Anonymous
Not applicable

Update: I think I am getting closer, but still not quite there. I created a new measure that generates a table as a variable with the most up-to-date values for each Id based on the date and dimension selected.

 

idCount = 

VAR selectedDate = SELECTEDVALUE(dateTable[date])

VAR selectedAxis = SELECTEDVALUE(historyTable[dimension])

VAR __table =

   ADDCOLUMNS(SUMMARIZE(historyTable, historyTable[Id],

        "maxDate" = CALCULATE(MAX(historyTable[dimDate]), 

              FILTER(historyTable, historyTable[dimDate]<=selectedDate

                        && historyTable[dimension] = selectedAxis))),

       "maxValue", MAXX(FILTER(historyTable, [dimDate] = [maxDate] 

                       && historyTable[dimension] = selectedAxis), historyTable[dimSelected])

RETURN

CALCULATE(SUMX(__table,

       IF(maxDate = CALCULATE(MAX(historyTable[dimDate],

                           FILTER(historyTable, historyTable[dimDate]<=selectedDate)), 1, 0)),

  ALLSELECTED(historyTable),

  FILTER(__table, [maxDate] <= selectedDate))

 

but I am still not getting the counts I am expecting when dropping the measure into a table with the values from 'historyTable'[dimSelected]

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.