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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Running totals with if else and max value

Hi,

 

I want to create a table about leave and need a cummulative total.

Actually I already using a measure in here.

And here is the result.

 

2017-09-12_1148.png

 

I want in the Annual Leave (Column or Measure) the data max 20. So it will be like this

2017-09-12_1158.png

 

p.s. the Annual Leave column is the column that I created manually and I expect the result as the Annual Leave column

 

I already try to create new column with this power query:

Annual Leave Column2 = 
IF((CALCULATE (
    SUM ( Sheet1[Debit/Credit] ),
    ALL ( Sheet1 ),
   Sheet1[Date] <= EARLIER ( Sheet1[Date] )
))>20, 20,CALCULATE (
    SUM ( Sheet1[Debit/Credit] ),
    ALL ( Sheet1 ),
   Sheet1[Date] <= EARLIER ( Sheet1[Date] )
))

But the result when it already 20 and there is a -1 in next date it will still count 20 and stuck in 20. 

 

 

Any help would be appreciate 🙂

 

Thanks,

Regards,

Connie

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Answered by @MarcelBeug in this link.

 

Thanks a lot for the answer and here is the answer

 

using power query in advanced editor with this query

let
    Source = Excel.Workbook(File.Contents("D:\Projects\Internal CRM\Leave Data.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Type", type text}, {"Debit/Credit", type number}}),
    RunningSum = List.Skip(List.Accumulate(#"Changed Type"[#"Debit/Credit"],{0},(sum,value) => sum & {List.Min({12,List.Last(sum)+value})})),
    TableWithRunningSum = Table.FromColumns(Table.ToColumns(#"Changed Type")&{RunningSum},Value.Type(Table.AddColumn(#"Changed Type","Running Sum", each 0, type number)))
in
    TableWithRunningSum

 And thanks for @CahabaData for helping me until I get this answer.

 

Thank you!

View solution in original post

21 REPLIES 21
Anonymous
Not applicable

Please help still stuck with this issue.

 

Any help would be greatly appreciated.

 

Thanks,

Regards,

Connie

If you create a basic Running Sum column/measure of the Debit/Credit field, it will go above 20.  Let that be ok.

 

Then you can use the IF measure of my first post against this new Running Sum column to create another new column/measure. 

 

Would this be the result you seek?

 

 

www.CahabaData.com
Anonymous
Not applicable

Hi @CahabaData

 

Thank you for replying.

If I use the measure, it will be stuck in 20 as you can see my attached picture.

2017-09-16_2238.png

 

This is in Power BI, as you can see the data will be more than 20. and for Column 2 I using your IF measure the result still stuck in 20 because it read the data is more than 20

 

But what I the result I need is like this one

2017-09-16_2236.png

 

As you can see, what I need is the data will be set into 20 so when there is a -1 it will changed into 19 not 20.

And if the data always become 20 and still summing then it's not the result what I seek. And the Annual Leave column is the column that I create manually (not using dax or any calculation) and I need the result as the Annual Leave column. I create it only for comparison if my calculation true or false.

 

Please let me know if you still don't understand about my requirements. You can download my pbix I already share it in this thread. 

 

Thanks,

Regards,
Connie

 

this might just work - adding another column with this logic

 

column 3 = IF( [Debit/Credit]<0 && [column 2] = 20, 20 - [Debit/Credit], [column 2] )

 

air code....not sure if it will increment correctly in all cases....

www.CahabaData.com
Anonymous
Not applicable

Still not work 😞

 

2017-09-16_2328.png

 

It become 21 instead.

 

yeah didn't think that one through

 

so I think one starts over and relies upon either EARLIER function or possibly the LOOKUPVALUE function  and just add the current D/C value to the earlier/prior row Annual Leave value

 

haven't hashed out the syntax but I think it is going to work

 

 

www.CahabaData.com

Annual Leave =

VAR currentDate = 'Table1'[Date]

VAR PreDate =

    CALCULATE (

        LASTDATE ( 'Table1'[Date] ),

        FILTER ( 'Table1', 'Table1'[Date] < currentDate )

    )

RETURN

    IF (

       LOOKUPVALUE ( 'Table1'[Value], Table1[Date], PreDate ) + [Debit/Credit] >20, 20,

           LOOKUPVALUE ( 'Table1'[Value], Table1[Date], PreDate ) + [Debit/Credit]

        ) )

 

by now you've learned to be wary of my air code - but this is a stab at it.......  even if this works you may have to wrap another piece of logic to get the initial 16 value....

www.CahabaData.com
Anonymous
Not applicable

Hi @CahabaData,

 

I already tried, and I don't understand what is the Table[Value]? is that I need to create another column that takes a previous value like this one?

a.png

 

And when using this value and your measure the result is like this one

b.png

 

"even if this works you may have to wrap another piece of logic to get the initial 16 value...." And I don't understand why we need to get the initial 16 value?

 

Thanks,
Regards,

Connie

Annual Leave =
VAR currentDate = 'Table1'[Date]
VAR PreDate =
    CALCULATE (
        LASTDATE ( 'Table1'[Date] ),
        FILTER ( 'Table1', 'Table1'[Date] < currentDate )
    )
RETURN
    IF (
       LOOKUPVALUE ( 'Table1'[Debit/Credit], 'Table1'[Date], PreDate ) + 'Table1'[Debit/Credit] >20, 20,
           LOOKUPVALUE ( 'Table1'[Debit/Credit], 'Table1'[Date], PreDate ) + 'Table1'[Debit/Credit]
        ) )

www.CahabaData.com
Anonymous
Not applicable

Hi @CahabaData,

 

Thank you for taking your time to thinking about the measure, but the result is really weird. Have you try to implement in my pbix file? Since I already shared it and you can download it.

The result is like this one.

 

2017-09-19_1001.png

 

Thanks,

Regards,

Connie

Anonymous
Not applicable

Anyone can help with this problem?

 

because what I think we need the function to refer its own column

yes that is what is needed - will open a new post on this topic....

@Anonymous

www.CahabaData.com
Anonymous
Not applicable

Answered by @MarcelBeug in this link.

 

Thanks a lot for the answer and here is the answer

 

using power query in advanced editor with this query

let
    Source = Excel.Workbook(File.Contents("D:\Projects\Internal CRM\Leave Data.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Type", type text}, {"Debit/Credit", type number}}),
    RunningSum = List.Skip(List.Accumulate(#"Changed Type"[#"Debit/Credit"],{0},(sum,value) => sum & {List.Min({12,List.Last(sum)+value})})),
    TableWithRunningSum = Table.FromColumns(Table.ToColumns(#"Changed Type")&{RunningSum},Value.Type(Table.AddColumn(#"Changed Type","Running Sum", each 0, type number)))
in
    TableWithRunningSum

 And thanks for @CahabaData for helping me until I get this answer.

 

Thank you!

CahabaData
Memorable Member
Memorable Member

Please try this for a new calculated column in your table:

 

New Annual Leave = IF([Annual Leave] >20, 20, [Annual Leave])

 

 

www.CahabaData.com
Anonymous
Not applicable

Hi @CahabaData,

 

Thanks for replying my question.

I already try your calculation but when it on 20 it will stuck on 20 not going to 19 if there is a -1. 

Please check my attached picture.

new.png

 

Thanks,

Regards,

Connie

Hi,

 

Try this calculated field formula

 

=MAX(20,CALCULATE(SUM(data[Debit/Credit]),FILTER(Data,Calendar[Date]<=MAX([Date]))))

Ensure that the Date field in your visual is dragged form the calendar table.

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @Ashish_Mathur,

 

Am I need to create the calendar table first?

 

If I'm using the date that already in data the result for the data are taking the data's date. Please refer to my attachment, I'm using the calculation in Column 2.

 

new_2.png

Hi,

 

Mine is a calculated field formula (not a calculated column formula).  Do you want a calculated column formula?

 

Please share the link from where i can download your file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @Ashish_Mathur

 

Here is the link:

Here

 

Actually Column/Measure is ok as long as it's running. I created column just to see the differences.

 

Thanks

Hi,

 

Please let me know the exact result which you are expecing in each cell.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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