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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
StevenHiatt
Frequent Visitor

Create a table visual to show calculations for different time periods

I would like to create a table visualization that would show total sales for different time periods, to look like the screenshot below, but have the totals calculate correctly for each period.

 

StevenHiatt_0-1645221899822.png

I have created a manual table 'Periods to Show' (screenshot below) to contain the time periods I want to show and units I think will be needed for the calculation.

 

StevenHiatt_1-1645222063323.png

 

I have the following metric, and want to somehow leverage the 'Number' and 'Unit' fields from the table above to feed into the DATESINPERIOD function (parts highlighted in red). 'Dates' is my calendar table, and 'Sales Data'[Start Date] is the last sale date - to be used as the starting point for the period calculations.

 

Period Sales =
CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], 'Sales Data'[Start Date], - 1, DAY)
)

 

Is it possible to swap out the highlighted parts with references to the 'Periods to Show' table to get the totals I want, or is this wishful thinking? Or is there a better way to accomplish what I am trying to do?

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@StevenHiatt , Try like, I have not tested it can take period as variable.

 

A measure like

 

Period Sales =
CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]), Max(Table[Unit]))
)

 

If it give error for period then write like

Period Sales = Switch (True(),

Max(Table[Unit]) = "Month",

CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]),Month )
),

Max(Table[Unit]) = "Year",CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]),Year)
),Max(Table[Unit]) = "Year",CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]),DAY)
)

)

 

Table is having those 7 days etc

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
StevenHiatt
Frequent Visitor

Thanks! The second version using SWITCH() worked. I added another section to include DAY and it worked exactly as hoped.

 

Much appreciated!

amitchandak
Super User
Super User

@StevenHiatt , Try like, I have not tested it can take period as variable.

 

A measure like

 

Period Sales =
CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]), Max(Table[Unit]))
)

 

If it give error for period then write like

Period Sales = Switch (True(),

Max(Table[Unit]) = "Month",

CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]),Month )
),

Max(Table[Unit]) = "Year",CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]),Year)
),Max(Table[Unit]) = "Year",CALCULATE(
SUM('Sales Data'[Total Sales]),
DATESINPERIOD(Dates[Date], Max('Sales Data'[Start Date]), - 1*max(Table[Number]),DAY)
)

)

 

Table is having those 7 days etc

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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.