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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

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

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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