Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

Custom column

Hi, so I have two columns depth from and depth to. I need to create a new column called Total depth that:

- has the first row the same as in depth from (30 in this case)

- the rest of rows are copies from the depth to column.

 

20 Replies

  • Anonymous you have to have an identifier to find the previous row, again as mentioned in the previous post, some index/id/date column to find out the row if you don't have this in the model you can add index column in power query.

     

    Let's be clear, it is not as straight forward as excel where you can refer to a cell.

     

    I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

    Visit us at https://perytus.com, your one-stop shop for Power BI related projects/training/consultancy.

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      parry2k Yes, here it is, sorry forgot to include it:

  • Anonymous ,

    Follow the below steps:

    Step 1: create index column from Power Query. Go to Power Query editor --> Select any column on which you want to create index lets say "DateColumn"  --> Go to Add Column menu --> click on Index Column --> Save and apply.

    Step 2: Create below DAX Column:

    Column =
    Var a = CALCULATE(MAX(Sheet6[Value2]),FILTER((Sheet6),Sheet6[Index]=EARLIER(Sheet6[Index])-1))
    RETURN IF(a=BLANK(),CALCULATE(SUM(Sheet6[Value]),Sheet6[Index]=0),a)
    See the below Screen shot:
    • Tahreem24's avatar
      Tahreem24
      Super User

      So directly use the calculated column like below:

      Column =
      Var a = CALCULATE(MAX(Sheet6[Value2]),FILTER((Sheet6),Sheet6[StepNo]=EARLIER(Sheet6[StepNo])-1))
      RETURN IF(a=BLANK(),CALCULATE(SUM(Sheet6[Value]),Sheet6[StepNo]=1), a)
       
    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Tahreem24  Sorry, forgot in include this column called step no:

       

  • v-alq-msft's avatar
    v-alq-msft
    Community Support

    Hi, Anonymous 

     

    Based on your description, you need to create an index column in 'Query Editor'. The pbix file is attached in the end.

     

    Then you may create a calculated column and a measurem, or only a measure.

    Calculated column:
    
    Column = 
    IF(
        [Index]=0,
        [Depth from],
        LOOKUPVALUE('Table'[Depth to],'Table'[Index],[Index]-1)
    )
    
    Measure:
    Measure 1 = 
    IF(
        ISFILTERED('Table'[Depth from]),
        SUM('Table'[Column]),
        LOOKUPVALUE('Table'[Depth to],'Table'[Index],CALCULATE(MAX('Table'[Index]),ALL('Table')))
    )

     

    or

     

    Measure 2 = 
    var _index = SELECTEDVALUE('Table'[Index])
    return
    IF(
        ISFILTERED('Table'[Depth from]),
        IF(
            _index=0,
            SELECTEDVALUE('Table'[Depth from]),
            LOOKUPVALUE('Table'[Depth to],'Table'[Index],_index-1)
        ),
        LOOKUPVALUE('Table'[Depth to],'Table'[Index],CALCULATE(MAX('Table'[Index]),ALL('Table')))
    )

     

    Result:

     

    Best Regards

    Allan

     

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi v-alq-msft  Sorry, forgot to include this column called step no:

       

      • v-alq-msft's avatar
        v-alq-msft
        Community Support

        Hi, Anonymous 

         

        You may create a measure as below. The pbix file is attached in the end.

        Result = 
        var _stepno = SELECTEDVALUE('Table'[Step No])
        return
        IF(
            ISFILTERED('Table'[Depth from]),
            IF(
                _stepno=1,
                SELECTEDVALUE('Table'[Depth from]),
                LOOKUPVALUE('Table'[Depth to],'Table'[Step No],_stepno-1)
            ),
            LOOKUPVALUE('Table'[Depth to],'Table'[Step No],CALCULATE(MAX('Table'[Step No]),ALL('Table')))
        )

         

        Result:

         

        Best Regards

        Allan

         

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

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Create a Calculated Column

     

    Total Depthm =
    SWITCH(
    TRUE(),
    'Table'[stepNo] = 1 , 'Table'[Depth from m],
    'Table'[stepNo] = 2, 'Table'[depth to m],
    CALCULATE(MAX('Table'[depth to m]),FILTER('Table','Table'[stepNo] < EARLIER('Table'[stepNo]))
    ) )
     
     

    Regards,
    Harsh Nathani

    Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Anonymous thanks!

      There are, however, to issues with this column:

      - md_to3 and md_to4 should be equal to Total Depthm4 and Total Depthm5 respectively

      - md_to16 (9726,71) is not copied/added to the Total Depthm column.

       

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

         

        Make sure that the values are in Don't Summarize form

         

        Use this formula for Calculated Column

         

        Total Depthm =
        SWITCH(
        TRUE(),
        'Table'[stepNo] = 1 , 'Table'[Depth from m],
        'Table'[stepNo] = 2, 'Table'[depth to m],
        CALCULATE(MAX('Table'[depth to m]),FILTER('Table','Table'[stepNo] = EARLIER('Table'[stepNo]) - 1))
        )
         
         
        Also add a blank row will step no to have the last value shown.
         
         
         
         
         

        Regards,
        Harsh Nathani

        Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)