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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Take the first values up to a certain level

Hello,

 

I have a rather tricky question I cannot solve myself.

 

I have this kind of recordings, which describe time slots  :

 

BeginningCategoryDuration (hours)
23/01/2020 09:00blabla6
23/01/2020 15:00blibli3
23/01/2020 18:00bloblo2

 

I have to create reports that only take 7.5 hours maximum per day so I would like to add a column that recalculates the durations so that their total reaches 7.5, starting with the first ones. Here's how it would look like :

 

Exemple :

 

BeginningCategoryDuration (hours)Duration (2)
23/01/2020 09:00blabla66
23/01/2020 15:00blibli31.5
23/01/2020 18:00bloblo20

 

I can do it by extracting the data and use Excel mais I would love to have it displayed directly in PowerBi.

 

Thank you for your help,

 

 

3 ACCEPTED SOLUTIONS

Hi @Anonymous,

 

In order to make it work, replace the bold part (it's my last step name).

Take a look at yours steps, get the name and replace it in the function. 

If possible share you m code, so I can replace it with your last step name.



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

Anonymous
Not applicable

Hello,

thank you so much for your answer. DAX is ideed an easier way for me to complete the task.

 

I dont have an index so I tested on the beginning date this way :

 

Measure = 
var _previousduration= CALCULATETABLE(VALUES('Table'[Duration (hours)]),FILTER(ALL('Table'),'Table'[Beginning]<MAX('Table'[Beginning])))Return
IF(SUMX(FILTER(ALL('Table'),'Table'[Beginning]<=MAX('Table'[Beginning])),'Table'[Duration (hours)])<=7.5,MAX('Table'[Duration (hours)]),7.5-SUMX(_previousduration,'Table'[Duration (hours)]))
It seemms to work except if I have only one task longer than 7.5h. In that case, the result is 0 (should be 7.5).
Capture.PNG
Is there any way you can think of that would solve the issue ?
 
Thank you
 

View solution in original post

Hi @Anonymous ,

 

You can add a criteria in the if function to achieve it.

 

Measure = 
var _previousduration= CALCULATETABLE(VALUES('Table'[Duration (hours)]),FILTER(ALL('Table'),'Table'[Index]<MAX('Table'[Index])))Return
IF(MAX('Table'[Duration (hours)])>=7.5,7.5,IF(SUMX(FILTER(ALL('Table'),'Table'[Index]<=MAX('Table'[Index])),'Table'[Duration (hours)])<=7.5,MAX('Table'[Duration (hours)]),7.5-SUMX(_previousduration,'Table'[Duration (hours)])))
Duration (2) = 
IF('Table'[Measure]>0,'Table'[Measure],0)

 

Finally you will see:

Annotation 2020-06-02 091312.png

For the modified .pbix file,pls click here.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

View solution in original post

8 REPLIES 8
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

You can also try to use dax expression to achieve it:

Create 2 measures as below:

 

Measure = 
var _previousduration= CALCULATETABLE(VALUES('Table'[Duration (hours)]),FILTER(ALL('Table'),'Table'[Index]<MAX('Table'[Index])))Return
IF(SUMX(FILTER(ALL('Table'),'Table'[Index]<=MAX('Table'[Index])),'Table'[Duration (hours)])<=7.5,MAX('Table'[Duration (hours)]),7.5-SUMX(_previousduration,'Table'[Duration (hours)]))
Duration (2) = 
IF('Table'[Measure]>0,'Table'[Measure],0)

 

Finally you will see:

Annotation 2020-06-01 164302.png

For the related .pbix file,pls click here.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!

 

 

Anonymous
Not applicable

Hello,

thank you so much for your answer. DAX is ideed an easier way for me to complete the task.

 

I dont have an index so I tested on the beginning date this way :

 

Measure = 
var _previousduration= CALCULATETABLE(VALUES('Table'[Duration (hours)]),FILTER(ALL('Table'),'Table'[Beginning]<MAX('Table'[Beginning])))Return
IF(SUMX(FILTER(ALL('Table'),'Table'[Beginning]<=MAX('Table'[Beginning])),'Table'[Duration (hours)])<=7.5,MAX('Table'[Duration (hours)]),7.5-SUMX(_previousduration,'Table'[Duration (hours)]))
It seemms to work except if I have only one task longer than 7.5h. In that case, the result is 0 (should be 7.5).
Capture.PNG
Is there any way you can think of that would solve the issue ?
 
Thank you
 
camargos88
Community Champion
Community Champion

Hi @Anonymous ,

 

Try this code for a custom column on Power Query:

 

let
_dateTime = [Beginning],
_currentValue = [#"Duration (hours)"],
_day = Table.SelectRows(#"Changed Type",
each
Date.From([Beginning]) = Date.From(_dateTime) and
[Beginning] <= _dateTime
)[#"Duration (hours)"],
_result =
List.Accumulate(_day, 7.5, (s,c) => s - c) in
if _result > 0 then _currentValue
else if _result + _currentValue > 0 then _result + _currentValue
else 0

 

change the bold part for your last step name.

 

Capture.PNG

 

 



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Hello, thank you very much for your quick answer.

I tried the solution but can't make it work.

I can add a custom column with the formula you provided but I don't understand what I should replace "changed type" with, nor do I get how to use the formula with "addTable" below.

Annotation 2020-05-30 192947.png

 

Could you just be a bit more descriptive please ?

best regards

Hi @Anonymous,

 

In order to make it work, replace the bold part (it's my last step name).

Take a look at yours steps, get the name and replace it in the function. 

If possible share you m code, so I can replace it with your last step name.



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Hello Camargos,

I think your solution works. I will mark it as a solution as soon I have an answer to the question I asked to the second person that proposed me a solution (with DAX, which is a bit easier for the newbie I am).

Thank you again,

Hi @Anonymous ,

 

You can add a criteria in the if function to achieve it.

 

Measure = 
var _previousduration= CALCULATETABLE(VALUES('Table'[Duration (hours)]),FILTER(ALL('Table'),'Table'[Index]<MAX('Table'[Index])))Return
IF(MAX('Table'[Duration (hours)])>=7.5,7.5,IF(SUMX(FILTER(ALL('Table'),'Table'[Index]<=MAX('Table'[Index])),'Table'[Duration (hours)])<=7.5,MAX('Table'[Duration (hours)]),7.5-SUMX(_previousduration,'Table'[Duration (hours)])))
Duration (2) = 
IF('Table'[Measure]>0,'Table'[Measure],0)

 

Finally you will see:

Annotation 2020-06-02 091312.png

For the modified .pbix file,pls click here.

 

Best Regards,
Kelly
Did I answer your question? Mark my post as a solution!
Anonymous
Not applicable

Hello

thank you very much. It works !

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.