Forum Discussion
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.
| DateWeek | max_item_value | Pessimism | Most likely | Optimistic | Date |
| 20201201-49 | 1811 | 1813 | 1814 | 1816 | 01/12/2020 |
| 20201202-49 | 1811 | 1813 | 1814 | 1816 | 02/12/2020 |
| 20201206-50 | 1811 | 1815 | 1817 | 1821 | 06/12/2020 |
| 20201207-50 | 1811 | 1815 | 1817 | 1821 | 07/12/2020 |
| 20201213-51 | 1811 | 1817 | 1820 | 1826 | 13/12/2020 |
| 20201214-51 | 1811 | 1817 | 1820 | 1826 | 14/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
Microsoft 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
- AnonymousNot 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
Community 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
Microsoft 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)*2Regards,Pat