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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
masplin
Impactful Individual
Impactful Individual

Struggling to get result I want from TopN function

I have a datset that looks like this

 

SubmittedDateTimeATEDateSubmittedTimeRunning total
17/11/2019 08:5717/11/2019 00:001899-12-30 08:57:15846
17/11/2019 09:0017/11/2019 00:001899-12-30 09:00:16847
17/11/2019 09:0117/11/2019 00:001899-12-30 09:01:16848
17/11/2019 11:1417/11/2019 00:001899-12-30 11:14:26849
17/11/2019 11:1517/11/2019 00:001899-12-30 11:15:42850
17/11/2019 11:2017/11/2019 00:001899-12-30 11:20:18851
17/11/2019 12:0117/11/2019 00:001899-12-30 12:01:34852
17/11/2019 12:2517/11/2019 00:001899-12-30 12:25:57853
17/11/2019 15:0517/11/2019 00:001899-12-30 15:05:25854
17/11/2019 15:5817/11/2019 00:001899-12-30 15:58:09855
17/11/2019 16:2517/11/2019 00:001899-12-30 16:25:39856
17/11/2019 16:5417/11/2019 00:001899-12-30 16:54:05857
17/11/2019 16:5917/11/2019 00:001899-12-30 16:59:25858
17/11/2019 17:1617/11/2019 00:001899-12-30 17:16:04859
17/11/2019 17:3317/11/2019 00:001899-12-30 17:33:49860
17/11/2019 17:4717/11/2019 00:001899-12-30 17:47:07861
17/11/2019 17:5717/11/2019 00:001899-12-30 17:57:14862
17/11/2019 19:2917/11/2019 00:001899-12-30 19:29:20863
17/11/2019 19:5717/11/2019 00:001899-12-30 19:57:18864
17/11/2019 20:3817/11/2019 00:001899-12-30 20:38:43865
17/11/2019 20:3917/11/2019 00:001899-12-30 20:39:32866
17/11/2019 21:1217/11/2019 00:001899-12-30 21:12:15867
17/11/2019 21:2917/11/2019 00:001899-12-30 21:29:36868
17/11/2019 21:3017/11/2019 00:001899-12-30 21:30:24869
17/11/2019 21:3117/11/2019 00:001899-12-30 21:31:18870
17/11/2019 21:5817/11/2019 00:001899-12-30 21:58:28871
17/11/2019 22:1517/11/2019 00:001899-12-30 22:15:15872
17/11/2019 23:1817/11/2019 00:001899-12-30 23:18:58873
17/11/2019 23:4017/11/2019 00:001899-12-30 23:40:54874

 

I want to be able to pickout what the rolling total was on each hour of each day.

I have a Date table and also a Time table

 

I wrote this measure

 

Measure 3 = 
VAR
MaxTime=MAX('Time'[Hour])
VAR
MaxDay=Max('Date'[Date])
RETURN
CALCULATE(VALUES(ATEs[Running total]),
                                   TOPN(1,
                                          CALCULATETABLE(ATEs,
                                                        ALL(ATEs[SubmittedTime]) ,
                                                        ATEs[SubmittedTime]<=MaxTime ,
                                                              ATEs[ATEDate]=MaxDay
                                                        ),
                                         ATEs[SubmittedDateTime],
                                         DESC
                                        )
                         )

 

 

and put it in this matrix with Date[Day] on the rows and Time[Hour] on the columns

Capture.JPG

So i have 2 issues that i can't work out

1. The number for 17/11 12 hours is the value at 12:25, but I was expecting I had created a table with only submitted time up to 12 and then taken the top row. Clearly this isnt happening and is taking submitted time up to where the hour is 12.  I tried wrapping the MaxTime in TIME(MaxTime,0,0) to force it to 12:00:00, but just blows up. given Time[Hour] is a number I'm not sure how it is doing the comparison with submittedtime which is a time. 

2. I have a blank for 10 because there are no records in the 10-11 bucket on 17/11. I tried using the ALL(Submitted time) because i can see the 10 on the column is creating a context that excludes the times before 10, but deons't seem to make any difference. The number here should be 848 the value at 9:02 as last value before 10:00:00. 

 

Appreciate any assitance

Mike

 

1 ACCEPTED SOLUTION
mwegener
Most Valuable Professional
Most Valuable Professional

Hi @masplin ,

 

the data should not be a problem, it just needs to be transformed.

You find the necessary steps in the PBIX

Changes.png

 

https://dax.guide/allselected/

https://dax.guide/isonorafter/

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


View solution in original post

8 REPLIES 8
mwegener
Most Valuable Professional
Most Valuable Professional

Hi @masplin 

 

check this out.

 

PBIX

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


masplin
Impactful Individual
Impactful Individual

Hi. I wish I could understand how this has managed to fill in the blanks!!!

 

  I assume ALLSELECTED(Time[time]) selects all the hours so removing column context?  I can't work out the syntax of the ISONORAFTER. Is Time[Time] the time each row in the data and Max(Time[time]) is the limit of the column its in i.e 10:59 for column 10? If so really don't understand as definition is "If the sort order is descending, the comparison to be done is second parameter less than or equal to first parameter."  So that would be true for rows in the date with a time after the hour in the column? ISONORAFTER with DESC the same as ISONORBEFORE?

 

I still have the same issue that the data is in the wrong columns. At 9am 847 are outstanding not 848 , at 11 848 are outstanding not 851. Actually cracked it need to use MIN(Time[Time]) so uses 10:00 in thr 10 bucket.

 

Thanks  for your help

 

Mike

mwegener
Most Valuable Professional
Most Valuable Professional

Hi @masplin ,

 

i updated the file.

Now every started hour is displayed in the next hour.

hour_up.png

 

The "Running total" calculation is the standard Power BI  Quick measure.

DAX does not knows a ISONORBEFORE.

But it is important that your Measure is aggregatable. (changes)

RunningTotal_QuickMeasure.png

 

PBIX

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


masplin
Impactful Individual
Impactful Individual

Ah I see the problem. I accidentally gave you a slice of data where the running total looks continuous.  The actual function behind this is not continuous so the next day looks like this.  Its a measure of incoming queries minus any ones dealt with so figure can go down. So a running total isn't possible. Henmce reason i was using TOPN

 

Capture.JPG

However the ALLSELECTED clause and ISONORAFTER are new to me so maybe will help me get the answer i need so appreciate your help

 

mwegener
Most Valuable Professional
Most Valuable Professional

Hi @masplin ,

 

the data should not be a problem, it just needs to be transformed.

You find the necessary steps in the PBIX

Changes.png

 

https://dax.guide/allselected/

https://dax.guide/isonorafter/

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


mwegener
Most Valuable Professional
Most Valuable Professional

Hi @masplin,

 

did you solve your problem?

 

If I answered your question, please mark my post as solution, this will also help others.

Please give Kudos for support.

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


masplin
Impactful Individual
Impactful Individual

Yes but using the TOPN version. your tip on ALLSELECTED and ONORBEFORE reallyhelped me solve it.

 

Sadly it turned out to be a completely pointless less calculation so I deleted it!

 

Mike 

mwegener
Most Valuable Professional
Most Valuable Professional

Hi @masplin,


then please mark one post as solution, this will help others to get this informations.

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast - Power BI Tutorials


Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors