Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Add constant to the previous value

Dear friends, good evening!

 

I need to build 3 columns from the value  in the 'max_item_value' column:

 

pessimism:

- is the value 1811 plus constant 2 totaling 1813 in week 49 (as you can see in the dateweek column).
- once we have the value 1813 generated in week 49, we will add again the constant 2 totaling 1815 in week 50.

 

most likely:
- is the value 1811 plus constant 3 totaling 1814 in week 49 (as you can see in the dateweek column).
- once we have the value 1814 generated in week 49, we will add again the constant 3 totaling 1817 in week 50.

 

optimistic:

- same logic as the others, but the constant to be added is 5.

 

DateWeekmax_item_valuePessimismMost likelyOptimisticDate
20201201-49181118131814181601/12/2020
20201202-49181118131814181602/12/2020
20201206-50181118151817182106/12/2020
20201207-50181118151817182107/12/2020
20201213-51181118171820182613/12/2020
20201214-51181118171820182614/12/2020

 

Could you please help me?

Thanks a lot!

  • Hi Anonymous ,

     

    You can create the following three calculated column:

     

    pessimism = 'Table'[max_item_value] + 2*(VALUE(RIGHT('Table'[DateWeek],2))-48)
    
    most likely = 'Table'[max_item_value]+3*(VALUE(RIGHT('Table'[DateWeek],2))-48)
    
    optimistic = 'Table'[max_item_value]+5*(VALUE(RIGHT('Table'[DateWeek],2))-48)

     

     

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

     

    Best Regards,

    Dedmon Dai

4 Replies

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    Here is one way to do it in the query editor with a custom column you can adapt as needed.  This could be done with 3 added columns, but I tried it with one and then expand it to make all 3.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.  Note that I first added a Week of Year column to be used in the calculation.

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bcrBCQAhDETRXnI2mIlRd2sR+29j3YsaCAz8w7wxSEUFa2wvJcIDrAgyNP8XzbSNeqORaVzlMi0y3ZseGBSuOAYlMuaNHTM/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateWeek = _t, max_item_value = _t, Date = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"DateWeek", type text}, {"max_item_value", Int64.Type}, {"Date", type text}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Date", type date}}, "en-150"),
        #"Inserted Week of Year" = Table.AddColumn(#"Changed Type with Locale", "Week of Year", each Date.WeekOfYear([Date]), Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Inserted Week of Year", "Custom", each let maxvalue = [max_item_value], woy = [Week of Year] in Record.FromList(List.Transform({2,3,5}, each maxvalue + (woy - 48)*_), {"Pessimism", "Most Likely", "Optimism"})),
        #"Expanded Custom" = Table.ExpandRecordColumn(#"Added Custom", "Custom", {"Pessimism", "Most Likely", "Optimism"}, {"Pessimism", "Most Likely", "Optimism"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Pessimism", Int64.Type}, {"Most Likely", Int64.Type}, {"Optimism", Int64.Type}})
    in
        #"Changed Type1"

     

     

    Regards,

    Pat

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Mahoney! Thank you so much for trying to help me. Do you know if it is possible to do this with DAX?

      • v-deddai1-msft's avatar
        v-deddai1-msft
        Icon for Community Support rankCommunity Support

        Hi Anonymous ,

         

        You can create the following three calculated column:

         

        pessimism = 'Table'[max_item_value] + 2*(VALUE(RIGHT('Table'[DateWeek],2))-48)
        
        most likely = 'Table'[max_item_value]+3*(VALUE(RIGHT('Table'[DateWeek],2))-48)
        
        optimistic = 'Table'[max_item_value]+5*(VALUE(RIGHT('Table'[DateWeek],2))-48)

         

         

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

         

        Best Regards,

        Dedmon Dai

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    It is possible with DAX too of course.  Still add the Week of Year column (in query or with a DAX column) and then use a column expression like this.  You can adapt for the other two columns.

     

    Pessimism2 = MaxItem[max_item_value] + (MaxItem[Week of Year]-48)*2
     
    Regards,
    Pat