Forum Discussion

jrdn03's avatar
jrdn03
Frequent Visitor
3 years ago
Solved

Creating Fiscal Year Date for Cost Fields

I'm not sure how to do this... And yes, the primary reason this is an issue is because the data elements were not defined like a database. I have multiple cost fields for each Fiscal Year, each re...
  • jgeddes's avatar
    3 years ago

    If you have data somewhat like...

    and you want...

    you can use Power Query to unpivot the cost fields with the dates in the titles, then split the column with the names so the cost type is seperated from the year. From there you can pivot the cost type column without aggregating the rows to get back to the original layout. 
    The following code shows the steps (you can paste it into the Advanced Editor of a blank query)

    let
    //manually created data table
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("LYy5EQAxCAN7IXZi8/gc3tMFQ/9tGHROFkarkTvd1KgPTqokRGfSJkVzesqxaCWw/T96/Js/r9IYWJehKkd/pUfHKmCMam5EbA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Project = _t, #"Salary Cost 2023-24" = _t, #"Capital Cost 2023-24" = _t, #"Salary Cost 2024-25" = _t, #"Capital Cost 2024-25" = _t]),
    //change data types
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", type text}, {"Salary Cost 2023-24", Int64.Type}, {"Capital Cost 2023-24", Int64.Type}, {"Salary Cost 2024-25", Int64.Type}, {"Capital Cost 2024-25", Int64.Type}}),
    //unpivot other columns, select all of the columns that are not cost fields and then choose unpivot other columns
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Project"}, "Attribute", "Value"),
    //split the cost fields into 2 columns by the space delimiter beween 'cost' and the 'year' portions of the column title
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, true), {"Cost Type", "Fiscal Year"}),
    //select the 'cost type' column and select pivot columns making sure to select do not aggregate.
    #"Pivoted Column" = Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[#"Cost Type"]), "Cost Type", "Value")
    in
    #"Pivoted Column"

    The actual solution to your issue will depend on the exact format/titles of your columns but hopefully this gets you going in the right direction.