Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago
Solved

DAX help

Hi,

I have data as mentioned below and wanted to create New Val calculated column, whenever there is negative make it zero and next row record should be substracted negative values.

 

In the below example, 1/1/2025 role 2 has -25 and it has to be zero in New Val and next row that is role 3 val has 40, but New Val should 40-25 i.e 15 should get it.

 

Not sure how to create calculated column to get values as I described.

 

Any help.

 

Thanks,
Thimma

 

DtCoderoleValNew Val
01-01-2025L113030
01-01-2025L12-250
01-01-2025L134015
01-02-2025L111515
01-02-2025L1200
01-02-2025L13-250
01-03-2025L11305
01-03-2025L12-50
01-03-2025L136560
01-04-2025L118383
01-04-2025L12-200
01-04-2025L134020
  • Hello Anonymous ,
    I can suggest quickly with the help of creating INDEX in Power Query. 

    DAX for the new column will be like below:

    New Value = 
    VAR PV = LOOKUPVALUE('Table'[Val],'Table'[Index],'Table'[Index]-1)
    RETURN
    IF('Table'[Val]<0,0,
    IF('Table'[Index] =1,'Table'[Val],
    IF('Table'[Val]>0 && PV < 0, 'Table'[Val] + PV , 'Table'[Val])))

    Output looks as below:

    Attached pbix as well for your refrence. 

    If this post helps, then please consider accepting it as the solution to help other members find it more quickly. Thank You!!

8 Replies

  • Hello Anonymous ,
    I can suggest quickly with the help of creating INDEX in Power Query. 

    DAX for the new column will be like below:

    New Value = 
    VAR PV = LOOKUPVALUE('Table'[Val],'Table'[Index],'Table'[Index]-1)
    RETURN
    IF('Table'[Val]<0,0,
    IF('Table'[Index] =1,'Table'[Val],
    IF('Table'[Val]>0 && PV < 0, 'Table'[Val] + PV , 'Table'[Val])))

    Output looks as below:

    Attached pbix as well for your refrence. 

    If this post helps, then please consider accepting it as the solution to help other members find it more quickly. Thank You!!

    • Anonymous's avatar
      Anonymous
      Not applicable

      Unfortunately I can't create Index, wanted to check using calculated column only.

      • Kishore_KVN's avatar
        Kishore_KVN
        Solution Sage

        No, that not right. You can create index without which how you know the order of the value column?

        So, you have to figure out a way of creating index for your data. Atleast ROW NUMBER should be created. 

  • Anonymous , Try using below dax for calculated column

     

    New Val =
    VAR CurrentVal = 'Table'[Val]
    VAR PreviousRow =
    CALCULATE(
    SUM('Table'[Val]),
    FILTER(
    'Table',
    'Table'[Dt] = EARLIER('Table'[Dt]) &&
    'Table'[role] = EARLIER('Table'[role]) - 1
    )
    )
    VAR Adjustment =
    IF(PreviousRow < 0, -PreviousRow, 0)
    RETURN
    IF(CurrentVal < 0, 0, CurrentVal - Adjustment)

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for the response, but previous val is not picking up and how will it take if any previous day has negative values. it should check current and previous date along with role as well.

  • v-priyankata's avatar
    v-priyankata
    Community Support

    Hi Anonymous 

    Thanks Kishore_KVN bhanu_gautam for your inputs, Anonymous May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

    Thank you.

  • v-priyankata's avatar
    v-priyankata
    Community Support

    Hi Anonymous 
    I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
    Thank you

  • v-priyankata's avatar
    v-priyankata
    Community Support

    Hi Anonymous 
    I hope this information is helpful. Please let me know if you have any further questions or if you'd like to discuss this further. If this answers your question, please Accept it as a solution and give it a 'Kudos' so others can find it easily.
    Thank you.