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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
hanumant_garad
Regular Visitor

How to calculate values based on condition to be be added in specified date range

Hi Power BI Community Support,

 

I am new to the power bi looking support for the below problem statement.

 

I have table as attached for that I wanted to created calculated column that should store values based on condition.

Condition:

e.g. in the highlighted row days_diff = 70 (difference between Start_Date and End_Date is 70 days) and country is C

so the condition is if days_diff >1 (here days_diff = 70) then 24 hours should be added for next consecutive days based on the value in days_diff (here next 70 days "hours" column should be filled with 24 hours) for country C only.

 

OR "calculated_column" / "hours" column rows should be filled with 24 hours between "Start_Date" and "End_Date"

As example shown in "calculated_column"

hanumant_garad_0-1628777661562.png

 

I hope the problem statement is clear and looking forward support.

thanks!

 

1 ACCEPTED SOLUTION
v-jayw-msft
Community Support
Community Support

Hi @hanumant_garad ,

 

Refer this:

Column 1 = IF('Table'[diff]>1,24)

Column 2 = 
var _lastkey = CALCULATE(MAX('Table'[key]),FILTER('Table','Table'[country]=EARLIER('Table'[country])&&'Table'[Column]=24&&'Table'[key]<EARLIER('Table'[key])))
var _lastdiff = CALCULATE(SUM('Table'[diff]),FILTER('Table','Table'[country]=EARLIER('Table'[country])&&'Table'[Column]=24&&'Table'[key]<EARLIER('Table'[key])))
return
IF('Table'[Column]=24,24,IF('Table'[key]<=_lastkey+_lastdiff,24,BLANK()))

 

Best Regards,

Jay

 

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.

View solution in original post

3 REPLIES 3
v-jayw-msft
Community Support
Community Support

Hi @hanumant_garad ,

 

Refer this:

Column 1 = IF('Table'[diff]>1,24)

Column 2 = 
var _lastkey = CALCULATE(MAX('Table'[key]),FILTER('Table','Table'[country]=EARLIER('Table'[country])&&'Table'[Column]=24&&'Table'[key]<EARLIER('Table'[key])))
var _lastdiff = CALCULATE(SUM('Table'[diff]),FILTER('Table','Table'[country]=EARLIER('Table'[country])&&'Table'[Column]=24&&'Table'[key]<EARLIER('Table'[key])))
return
IF('Table'[Column]=24,24,IF('Table'[key]<=_lastkey+_lastdiff,24,BLANK()))

 

Best Regards,

Jay

 

Community Support Team _ Jay
If this post helps, then please consider Accept it as the solution
to help the other members find it.
selimovd
Super User
Super User

Hey @hanumant_garad ,

 

your description is a little confusing.

Is that what you want:

calculated_column =
IF(
    myTable[days_diff] > 1,
    24
)

 

Otherwise can you please tell again in a more clear way what the condistions are?

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

@selimovd 

 

thanks for your quick response. 

the provided query will be fill 24 hours to that row only.  I wanted to add that 24hours to next days also (whatever days are in "days_diff" column).

 

this 24 hours should be populated to all rows between Start_Date and End_Date from that record where days_diff >1 and for that "country" only
just like I shown in "calculated_column" from the snapshot.

thanks!

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Power BI Header Change

Power BI Header Update

Read about the exciting updates for the Power BI forum.

Top Solution Authors