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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Patop
Frequent Visitor

Salary depending on year

Hello,

I have 4 columns in a table corresponding to the salary of 2018, the salary of 2019 until 202 depending on differents persons.

I also have a table that contains dates from 2018 to 2021.

I would like to create a mesure that will sum and show the salary depending on my column dates.

Like if the date is 2021, I sum the columns salary of 2021, if the date is 2018, I do the same but with the column 2018

Hence i would like somethink like this

Patop_0-1650357218495.png

Thank you very much

Have a good day

1 ACCEPTED SOLUTION
PC2790
Community Champion
Community Champion

Here you go:

Steps to be followed on high level:

  1. Unpivot all columns except "ID"
  2. Split the new "Attribute" column using the " " delimiter. It will create two columns -Attribute and Year
  3. Re-pivot the "Attribute" column.

M code as below:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dU9BDsAgCPsLZw846j5D/P83hsPEdM4DaRtKU9ylSpGqqgFXwkcNGmMxkF78VXMJsuJ0MZjl0loCqf0CwchJYve31Rv0xbHTveJA4fjt1B8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Salary 2018" = _t, #"Salary 2019" = _t, #"Salary 2020" = _t, #"Salary 2021" = _t, #"Income 2018" = _t, #"Income 2019" = _t, #"Income 2020" = _t, #"Income 2021" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute", "Year"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

Table looks like:

PC2790_1-1650443238546.png

 

 And simply visualise it in the form of a table:

PC2790_0-1650443041351.png

I hope this works for you.

 

Please provide a Kudos to this answer if you found it interesting.

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

View solution in original post

6 REPLIES 6
PC2790
Community Champion
Community Champion

Have a DateTable with which you can create relationship with your Main Table.

Create a measure called sum of Salary like this 

Salary Sum = Sum(MainTable[Salary]) and then visualise it in your report against Year from DatesTable like below:

PC2790_1-1650359599734.pngPC2790_2-1650359613093.png

 

Patop
Frequent Visitor

Hello,

Thanks for your answer but I have 4columns of salary so I just can't sum them i think, for exemple I have

Patop_0-1650359898714.png

and I would like to use my second table where I have dates to be able to have this:

Patop_1-1650359988597.png

Where the right columns is the sum of salary corresponding to the year

PC2790
Community Champion
Community Champion

Ok..Now that makes the problem much clearer.

You need to unpivot the columns here.

Go to Power Query and go to Advanced Editor and paste this code there after removing the exisiting code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VY7BDcAwCAN34Z0HCU6XQey/RklRCryswwajSpMGTWZ2WSGVbCittNCCuAlxkJjJDil0EnBoToHj7+xF++LveHIX7dJHZi8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Salary 2018" = _t, #"Salary 2019" = _t, #"Salary 2020" = _t, #"Salary 2021" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Salary 2018", Int64.Type}, {"Salary 2019", Int64.Type}, {"Salary 2020", Int64.Type}, {"Salary 2021", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Columns", "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Attribute.1"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Attribute.2", "Year"}, {"Value", "Salary"}})
in
    #"Renamed Columns"

The table would look like this:

PC2790_0-1650374811778.png

You can now visualise it in the form of a table as below:

PC2790_1-1650374885239.png

 

Patop
Frequent Visitor

Thank you so much!

and in case I have this :

Patop_0-1650387340440.png

 

and I want to have this :

Patop_1-1650387353183.png

What should I change in the code in order to have this result?

I tried but I didn't succeed

Thank you!

PC2790
Community Champion
Community Champion

Here you go:

Steps to be followed on high level:

  1. Unpivot all columns except "ID"
  2. Split the new "Attribute" column using the " " delimiter. It will create two columns -Attribute and Year
  3. Re-pivot the "Attribute" column.

M code as below:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dU9BDsAgCPsLZw846j5D/P83hsPEdM4DaRtKU9ylSpGqqgFXwkcNGmMxkF78VXMJsuJ0MZjl0loCqf0CwchJYve31Rv0xbHTveJA4fjt1B8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Salary 2018" = _t, #"Salary 2019" = _t, #"Salary 2020" = _t, #"Salary 2021" = _t, #"Income 2018" = _t, #"Income 2019" = _t, #"Income 2020" = _t, #"Income 2021" = _t]),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute", "Year"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type1", List.Distinct(#"Changed Type1"[Attribute]), "Attribute", "Value")
in
    #"Pivoted Column"

Table looks like:

PC2790_1-1650443238546.png

 

 And simply visualise it in the form of a table:

PC2790_0-1650443041351.png

I hope this works for you.

 

Please provide a Kudos to this answer if you found it interesting.

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

Patop
Frequent Visitor

Thank you that's perfect

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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