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 September 15. Request your voucher.

Reply
SimonLu
Regular Visitor

How to summarize table for 12 hour data on every hour

Hi, There

  i like to seeking help for below funciton :

  We have a talbe to record case data , i like to summarize those data group by Case class; Grade on previous 12 hour interval , and calculate evey hours.

Data Sample:

SimonLu_0-1754618404711.png

Result Sample

SimonLu_1-1754618578746.png

 

 

 

1 ACCEPTED SOLUTION
Sandip_Palit
Resolver II
Resolver II

This single DAX formula will create the entire summary table you need.

  • In Power BI Desktop, navigate to the Data view or Report view.
  • From the Modeling tab on the ribbon, click New Table.
  • Paste the following DAX formula into the formula bar. Be sure to replace 'Source Data' with the actual name of your data table.


Result Table =
// This formula generates a summary for every hour, calculating the sum of weight
// for each Case class and Grade over the preceding 12-hour window.

// Step 1: Create a table of every unique combination of Case class and Grade.
VAR GroupingKeys =
SUMMARIZE ( 'Source Data', 'Source Data'[Case class], 'Source Data'[Grade] )

// Step 2: Create a table containing every single hour between the first and last transaction.
VAR MinDateTime =
MIN ( 'Source Data'[Red Data] )
VAR MaxDateTime =
MAX ( 'Source Data'[Red Data] )
VAR HourlyTimePoints =
GENERATESERIES (
DATE ( YEAR ( MinDateTime ), MONTH ( MinDateTime ), DAY ( MinDateTime ) )
+ TIME ( HOUR ( MinDateTime ), 0, 0 ),
MaxDateTime,
TIME ( 1, 0, 0 )
)
VAR HourlyBins =
SELECTCOLUMNS ( HourlyTimePoints, "End date", [Value] )

// Step 3: Create the final table structure by combining every hourly point with every group.
VAR FinalTableShell =
CROSSJOIN ( HourlyBins, GroupingKeys )

// Step 4: Calculate the rolling sum for each row and format the output.
RETURN
SELECTCOLUMNS (
FILTER (
ADDCOLUMNS (
FinalTableShell,
"Weight",
VAR EndWindow = [End date]
VAR StartWindow = EndWindow - TIME ( 12, 0, 0 )
RETURN
CALCULATE (
SUM ( 'Source Data'[Weight (Net)] ),
KEEPFILTERS ( 'Source Data'[Case class] = EARLIER ( [Case class] ) ),
KEEPFILTERS ( 'Source Data'[Grade] = EARLIER ( [Grade] ) ),
'Source Data'[Red Data] > StartWindow
&& 'Source Data'[Red Data] <= EndWindow
)
),
[Weight] > 0
),
"Date", FORMAT ( [End date], "M/d/yyyy h:mm" ),
"Start Date", [End date] - TIME ( 12, 0, 0 ),
"End date", [End date],
"Case class", [Case class],
"Grade", [Grade],
"Weight", "Sum(Weight)" -- This is just a text label as in your example
)

 


If this explanation and solution resolve your issue, please like and accept the solution.

View solution in original post

2 REPLIES 2
Sandip_Palit
Resolver II
Resolver II

This single DAX formula will create the entire summary table you need.

  • In Power BI Desktop, navigate to the Data view or Report view.
  • From the Modeling tab on the ribbon, click New Table.
  • Paste the following DAX formula into the formula bar. Be sure to replace 'Source Data' with the actual name of your data table.


Result Table =
// This formula generates a summary for every hour, calculating the sum of weight
// for each Case class and Grade over the preceding 12-hour window.

// Step 1: Create a table of every unique combination of Case class and Grade.
VAR GroupingKeys =
SUMMARIZE ( 'Source Data', 'Source Data'[Case class], 'Source Data'[Grade] )

// Step 2: Create a table containing every single hour between the first and last transaction.
VAR MinDateTime =
MIN ( 'Source Data'[Red Data] )
VAR MaxDateTime =
MAX ( 'Source Data'[Red Data] )
VAR HourlyTimePoints =
GENERATESERIES (
DATE ( YEAR ( MinDateTime ), MONTH ( MinDateTime ), DAY ( MinDateTime ) )
+ TIME ( HOUR ( MinDateTime ), 0, 0 ),
MaxDateTime,
TIME ( 1, 0, 0 )
)
VAR HourlyBins =
SELECTCOLUMNS ( HourlyTimePoints, "End date", [Value] )

// Step 3: Create the final table structure by combining every hourly point with every group.
VAR FinalTableShell =
CROSSJOIN ( HourlyBins, GroupingKeys )

// Step 4: Calculate the rolling sum for each row and format the output.
RETURN
SELECTCOLUMNS (
FILTER (
ADDCOLUMNS (
FinalTableShell,
"Weight",
VAR EndWindow = [End date]
VAR StartWindow = EndWindow - TIME ( 12, 0, 0 )
RETURN
CALCULATE (
SUM ( 'Source Data'[Weight (Net)] ),
KEEPFILTERS ( 'Source Data'[Case class] = EARLIER ( [Case class] ) ),
KEEPFILTERS ( 'Source Data'[Grade] = EARLIER ( [Grade] ) ),
'Source Data'[Red Data] > StartWindow
&& 'Source Data'[Red Data] <= EndWindow
)
),
[Weight] > 0
),
"Date", FORMAT ( [End date], "M/d/yyyy h:mm" ),
"Start Date", [End date] - TIME ( 12, 0, 0 ),
"End date", [End date],
"Case class", [Case class],
"Grade", [Grade],
"Weight", "Sum(Weight)" -- This is just a text label as in your example
)

 


If this explanation and solution resolve your issue, please like and accept the solution.

Thanks, its working perfect. 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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