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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
edlng
Regular Visitor

How to create periods with date ranges

Hi!
I'm just starting PowerBI and need some help on a formula. I have five columns: site, date, energy, consumption, average unit price and cost.

I would like to create 6 different periods with ranges of dates, like May 2021 to July 2022 for instance. 

 

How can I do this to have one row per site and type of energy, with consumption and cost totals; and average unit price for each period?

For example:
London | May 2021-July 2022 | Electricity | total consumption over the period | average unit price over the period | total cost over the period
London | May 2021-July 2022 | Gas | total consumption otp | average unit price otp | total cost otp

 

Mainly, is it possible to do it with a formula?

 

I followed courses but didn't find the answer; that's why I wanted to ask here.

Thank you!

3 ACCEPTED SOLUTIONS
BeaBF
Super User
Super User

@edlng Hi! Here the steps:

 

1. Create a table with date ranges:

Periods =
DATATABLE(
"Period", STRING,
"Start Date", DATE,
"End Date", DATE,
{
{"May 2021 - July 2022", DATE(2021,5,1), DATE(2022,7,31)},
{"Aug 2022 - Dec 2022", DATE(2022,8,1), DATE(2022,12,31)},
{"Jan 2023 - Jun 2023", DATE(2023,1,1), DATE(2023,6,30)}
-- Add more periods here
}
)

 

2. Now, you need to create a relationship:
Go to Model View and connect the Periods[Start Date] and Periods[End Date] to your main table's Date column.

 

3. Calculates these measures:

Total Consumption =
CALCULATE(
SUM('YourTable'[Consumption]),
TREATAS(
FILTER(Periods,
'YourTable'[Date] >= Periods[Start Date] &&
'YourTable'[Date] <= Periods[End Date]
),
'YourTable'[Date]
)
)

 

Avg Unit Price =
DIVIDE(
SUMX('YourTable', 'YourTable'[Consumption] * 'YourTable'[Average Unit Price]),
SUM('YourTable'[Consumption])
)

 

Total Cost =
CALCULATE(
SUM('YourTable'[Cost]),
TREATAS(
FILTER(Periods,
'YourTable'[Date] >= Periods[Start Date] &&
'YourTable'[Date] <= Periods[End Date]
),
'YourTable'[Date]
)
)

 

4. Add a Matrix Visual.

Place:
Rows: Site, Period, Energy Type
Columns: (None, since you want it by row)
Values: Total Consumption, Avg Unit Price, Total Cost

 

BBF

View solution in original post

@edlng My code is to create a Calculated Table in DAX. You're in Power Query trying to calculate a custom column. Exit the power query, and click on Modeling > new Table

BeaBF_0-1743605156011.png

 

 

BBF

View solution in original post

v-bmanikante
Community Support
Community Support

Hi @edlng,

 

we would like to follow up to see if the solution provided by the super user resolved your issue. Please let us know if you need any further assistance.


If our super user response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

Regards,
B Manikanteswara Reddy

View solution in original post

4 REPLIES 4
v-bmanikante
Community Support
Community Support

Hi @edlng,

 

we would like to follow up to see if the solution provided by the super user resolved your issue. Please let us know if you need any further assistance.


If our super user response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

Regards,
B Manikanteswara Reddy

edlng
Regular Visitor

Thank you for your answer @BeaBF!
An error message popped up when I tried the first formula:

edlng_2-1743600838591.png

Do you know what went wrong?

 

 

@edlng My code is to create a Calculated Table in DAX. You're in Power Query trying to calculate a custom column. Exit the power query, and click on Modeling > new Table

BeaBF_0-1743605156011.png

 

 

BBF

BeaBF
Super User
Super User

@edlng Hi! Here the steps:

 

1. Create a table with date ranges:

Periods =
DATATABLE(
"Period", STRING,
"Start Date", DATE,
"End Date", DATE,
{
{"May 2021 - July 2022", DATE(2021,5,1), DATE(2022,7,31)},
{"Aug 2022 - Dec 2022", DATE(2022,8,1), DATE(2022,12,31)},
{"Jan 2023 - Jun 2023", DATE(2023,1,1), DATE(2023,6,30)}
-- Add more periods here
}
)

 

2. Now, you need to create a relationship:
Go to Model View and connect the Periods[Start Date] and Periods[End Date] to your main table's Date column.

 

3. Calculates these measures:

Total Consumption =
CALCULATE(
SUM('YourTable'[Consumption]),
TREATAS(
FILTER(Periods,
'YourTable'[Date] >= Periods[Start Date] &&
'YourTable'[Date] <= Periods[End Date]
),
'YourTable'[Date]
)
)

 

Avg Unit Price =
DIVIDE(
SUMX('YourTable', 'YourTable'[Consumption] * 'YourTable'[Average Unit Price]),
SUM('YourTable'[Consumption])
)

 

Total Cost =
CALCULATE(
SUM('YourTable'[Cost]),
TREATAS(
FILTER(Periods,
'YourTable'[Date] >= Periods[Start Date] &&
'YourTable'[Date] <= Periods[End Date]
),
'YourTable'[Date]
)
)

 

4. Add a Matrix Visual.

Place:
Rows: Site, Period, Energy Type
Columns: (None, since you want it by row)
Values: Total Consumption, Avg Unit Price, Total Cost

 

BBF

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

"); $(".slidesjs-pagination" ).prependTo(".pagination_sec"); $(".slidesjs-pagination" ).append("
"); $(".slidesjs-play.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-stop.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-pagination" ).append(""); $(".slidesjs-pagination" ).append(""); } catch(e){ } /* End: This code is added by iTalent as part of iTrack COMPL-455 */ $(".slidesjs-previous.slidesjs-navigation").attr('tabindex', '0'); $(".slidesjs-next.slidesjs-navigation").attr('tabindex', '0'); /* start: This code is added by iTalent as part of iTrack 1859082 */ $('.slidesjs-play.slidesjs-navigation').attr('id','playtitle'); $('.slidesjs-stop.slidesjs-navigation').attr('id','stoptitle'); $('.slidesjs-play.slidesjs-navigation').attr('role','tab'); $('.slidesjs-stop.slidesjs-navigation').attr('role','tab'); $('.slidesjs-play.slidesjs-navigation').attr('aria-describedby','tip1'); $('.slidesjs-stop.slidesjs-navigation').attr('aria-describedby','tip2'); /* End: This code is added by iTalent as part of iTrack 1859082 */ }); $(document).ready(function() { if($("#slides .item").length < 2 ) { /* Fixing Single Slide click issue (commented following code)*/ // $(".item").css("left","0px"); $(".item.slidesjs-slide").attr('style', 'left:0px !important'); $(".slidesjs-stop.slidesjs-navigation").trigger('click'); $(".slidesjs-previous").css("display", "none"); $(".slidesjs-next").css("display", "none"); } var items_length = $(".item.slidesjs-slide").length; $(".slidesjs-pagination-item > button").attr("aria-setsize",items_length); $(".slidesjs-next, .slidesjs-pagination-item button").attr("tabindex","-1"); $(".slidesjs-pagination-item button").attr("role", "tab"); $(".slidesjs-previous").attr("tabindex","-1"); $(".slidesjs-next").attr("aria-hidden","true"); $(".slidesjs-previous").attr("aria-hidden","true"); $(".slidesjs-next").attr("aria-label","Next"); $(".slidesjs-previous").attr("aria-label","Previous"); //$(".slidesjs-stop.slidesjs-navigation").attr("role","button"); //$(".slidesjs-play.slidesjs-navigation").attr("role","button"); $(".slidesjs-pagination").attr("role","tablist").attr("aria-busy","true"); $("li.slidesjs-pagination-item").attr("role","list"); $(".item.slidesjs-slide").attr("tabindex","-1"); $(".item.slidesjs-slide").attr("aria-label","item"); /*$(".slidesjs-stop.slidesjs-navigation").on('click', function() { var itemNumber = parseInt($('.slidesjs-pagination-item > a.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); });*/ $(".slidesjs-stop.slidesjs-navigation, .slidesjs-pagination-item > button").on('click keydown', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); var itemNumber = parseInt($('.slidesjs-pagination-item > button.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); }); $(".slidesjs-play.slidesjs-navigation").on('click', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); }); $(".slidesjs-pagination-item button").keyup(function(e){ var keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); $(".slidesjs-stop.slidesjs-navigation").trigger('click').blur(); $("button.active").focus(); } }); $(".slidesjs-play").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-stop").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-play")) { $(".slidesjs-stop").focus(); } } }); $(".slidesjs-stop").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-play").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-stop")) { $(".slidesjs-play").focus(); } } }); $(".slidesjs-pagination-item").keydown(function(e){ switch (e.which){ case 37: //left arrow key $(".slidesjs-previous.slidesjs-navigation").trigger('click'); e.preventDefault(); break; case 39: //right arrow key $(".slidesjs-next.slidesjs-navigation").trigger('click'); e.preventDefault(); break; default: return; } $(".slidesjs-pagination-item button.active").focus(); }); }); // Start This code is added by iTalent as part of iTrack 1859082 $(document).ready(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); $("#tip2").attr("aria-hidden","true").addClass("hidden"); $(".slidesjs-stop.slidesjs-navigation, .slidesjs-play.slidesjs-navigation").attr('title', ''); $("a#playtitle").focus(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").mouseover(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").blur(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#playtitle").mouseleave(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#play").keydown(function(ev){ if (ev.which ==27) { $("#tip1").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); $("a#stoptitle").focus(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").mouseover(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").blur(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").mouseleave(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").keydown(function(ev){ if (ev.which ==27) { $("#tip2").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); }); // End This code is added by iTalent as part of iTrack 1859082
Top Kudoed Authors