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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
wfo
Regular Visitor

Calculate yearly units based on date range

Dear Community,

 

I have a database which shows me consumed units based on a begin and end date. The table 'Consumption' looks like this:

IDBeginEndConsumedUnits
109/10/201809/14/20191458
209/15/201910/02/20202435
310/03/202010/01/20212100
410/02/202109/26/20222801

 

I would like to calculate and visualize the Consumed Units for each year (2018, 2019, 2020). The consumed units should for each year should be calculated with the average per day beween start and end date.

For 2018: timespan = 370 days; 3.94 consumed units per day in average; timespan until 12/31/2018 = 113 days; result is 445.28 consumed units in 2018

 

I tried with a DAX table but my approach was not that successful.

 

Thanks in advance for any assistance,

Wolfgang

1 ACCEPTED SOLUTION
mohdasaad94
Helper I
Helper I

You can use DAX to calculate the yearly consumed units based on the date range in the 'Consumption' table. Here's how you can do it:

  1. Create a new calculated column in the 'Consumption' table that calculates the number of days between the begin and end dates:

Days = DATEDIFF(Consumption[Begin], Consumption[End], DAY) + 1

The "+ 1" is added to include the end date in the calculation.

  1. Create another calculated column that calculates the daily average consumed units:

AvgUnitsPerDay = Consumption[ConsumedUnits] / Consumption[Days]

  1. Create a new table that contains the years you want to calculate the consumed units for:

Years = DISTINCT(YEAR(Consumption[Begin]))

This will give you a table with one column called "Years" that contains the distinct years from the 'Begin' column in the 'Consumption' table.

  1. Create a calculated column in the 'Years' table that calculates the consumed units for each year:

ConsumedUnits = VAR SelectedYear = Years[Years] RETURN SUMX( FILTER(Consumption, YEAR(Consumption[Begin]) = SelectedYear), IF( YEAR(Consumption[Begin]) = SelectedYear, IF( YEAR(Consumption[End]) = SelectedYear, Consumption[ConsumedUnits], Consumption[Days] * Consumption[AvgUnitsPerDay] ), IF( YEAR(Consumption[End]) = SelectedYear, (DATEDIFF(DATE(YEAR(Consumption[End]), 1, 1), Consumption[End], DAY) + 1) * Consumption[AvgUnitsPerDay], (DATEDIFF(DATE(YEAR(Consumption[Begin]), 12, 31), Consumption[Begin], DAY) + 1) * Consumption[AvgUnitsPerDay] ) ) )

This formula uses the SelectedYear variable to filter the 'Consumption' table to include only the records that fall within the selected year. It then uses an IF statement to calculate the consumed units for each record based on whether the begin and end dates fall within the selected year or not. If they do, it simply uses the consumed units value. If not, it calculates the consumed units by multiplying the number of days in the selected year by the daily average consumed units.

  1. Create a visual that uses the 'Years' table and the 'ConsumedUnits' column to display the yearly consumed units.

This should give you the yearly consumed units based on the date range in the 'Consumption' table.

View solution in original post

1 REPLY 1
mohdasaad94
Helper I
Helper I

You can use DAX to calculate the yearly consumed units based on the date range in the 'Consumption' table. Here's how you can do it:

  1. Create a new calculated column in the 'Consumption' table that calculates the number of days between the begin and end dates:

Days = DATEDIFF(Consumption[Begin], Consumption[End], DAY) + 1

The "+ 1" is added to include the end date in the calculation.

  1. Create another calculated column that calculates the daily average consumed units:

AvgUnitsPerDay = Consumption[ConsumedUnits] / Consumption[Days]

  1. Create a new table that contains the years you want to calculate the consumed units for:

Years = DISTINCT(YEAR(Consumption[Begin]))

This will give you a table with one column called "Years" that contains the distinct years from the 'Begin' column in the 'Consumption' table.

  1. Create a calculated column in the 'Years' table that calculates the consumed units for each year:

ConsumedUnits = VAR SelectedYear = Years[Years] RETURN SUMX( FILTER(Consumption, YEAR(Consumption[Begin]) = SelectedYear), IF( YEAR(Consumption[Begin]) = SelectedYear, IF( YEAR(Consumption[End]) = SelectedYear, Consumption[ConsumedUnits], Consumption[Days] * Consumption[AvgUnitsPerDay] ), IF( YEAR(Consumption[End]) = SelectedYear, (DATEDIFF(DATE(YEAR(Consumption[End]), 1, 1), Consumption[End], DAY) + 1) * Consumption[AvgUnitsPerDay], (DATEDIFF(DATE(YEAR(Consumption[Begin]), 12, 31), Consumption[Begin], DAY) + 1) * Consumption[AvgUnitsPerDay] ) ) )

This formula uses the SelectedYear variable to filter the 'Consumption' table to include only the records that fall within the selected year. It then uses an IF statement to calculate the consumed units for each record based on whether the begin and end dates fall within the selected year or not. If they do, it simply uses the consumed units value. If not, it calculates the consumed units by multiplying the number of days in the selected year by the daily average consumed units.

  1. Create a visual that uses the 'Years' table and the 'ConsumedUnits' column to display the yearly consumed units.

This should give you the yearly consumed units based on the date range in the 'Consumption' table.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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