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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Andyb431
Frequent Visitor

Extracting Values From a List & Inserting in Table at Specific Rows

Hi,

 

Link to File: Excel File 

 

I am trying to create a combined table from 2 source tables (Base_AnalysisTimeline and Benefits_Calc). I want to take the list of dates from the Base_AnalysisTimeline and have them repeat for every unique Benefit GUID from Benefits_Calc. I've got this part working. What I am struggling with is now inserting the Benefit GUID at each point where the row repeat begins, ie:

 

Benefit GUID

Count

Compound...

Discount Factor...

U1B1

0

 

 

 

1

 

 

 

2

 

 

 

3

 

 

 

ETC

 

 

U1B2

0

 

 

 

1

 

 

 

2

 

 

 

3

 

 

 

ETC

 

 

U1B3

0

 

 

 

1

 

 

 

ETC

 

 

 

I had created the testFunction1 to generate the list of distinct GUIDs and then to loop through and update the value it pulls from the list based on the index. This would then provide the next GUID in the list when the Count = 0. There is something going wrong, and in the table when I invoke the function I get:

"Expression.Error: We cannot convert the value 4 to type Function.
Details:
Value=4
Type=[Type]"

 

This is my first time doing any M coding and working with power query, so any possible help would be greatly appreciated.

 

Thanks in advance!

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Andyb431 

 

try this approach. Use your BenefitGUID-table and add a new column where you inserting you Timeline-table on every row. After that you expand the table

 

let
    Base_AnalysisTimeline=
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagkkzMGkOJi2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Count = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Count", Int64.Type}})
    in
        #"Changed Type",
    
    BenefitGUID = #table({"Benefit GUID"}, {{"U1B1"}, {"U1B2"}, {"U1B3"}}),

    Multiply = Table.AddColumn
    (
        BenefitGUID,
        "BaseAnalysisTimeLine",
        each Base_AnalysisTimeline
    ),
    Expand = Table.ExpandTableColumn(Multiply, "BaseAnalysisTimeLine", {"Count"}, {"Count"})
in  
    Expand

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

5 REPLIES 5
Jimmy801
Community Champion
Community Champion

Hello @Andyb431 

 

try this approach. Use your BenefitGUID-table and add a new column where you inserting you Timeline-table on every row. After that you expand the table

 

let
    Base_AnalysisTimeline=
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSBEyagkkzMGkOJi2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Count = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Count", Int64.Type}})
    in
        #"Changed Type",
    
    BenefitGUID = #table({"Benefit GUID"}, {{"U1B1"}, {"U1B2"}, {"U1B3"}}),

    Multiply = Table.AddColumn
    (
        BenefitGUID,
        "BaseAnalysisTimeLine",
        each Base_AnalysisTimeline
    ),
    Expand = Table.ExpandTableColumn(Multiply, "BaseAnalysisTimeLine", {"Count"}, {"Count"})
in  
    Expand

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Hi @Jimmy801 ,

 

Here is what I ended up with (note that I added a couple of columns on the right for additional values too):

 

let
    //Setup Base Table with GUIDs and Count
    Table_Setup1 =
        let
        //Get the source table and update the types
        Base_AnalysisTimeline =
        let
            Source = Excel.CurrentWorkbook(){[Name="Base_AnalysisTimeline"]}[content],
            #"Changed Type" = Table.TransformColumnTypes(Base_AnalysisTimeline,{{"Count", Int64.Type}})
        in
            #"Changed Type",
        
        
        //Create a table of the GUIDs
        BenefitGUID = Table.FromList(
            List.Distinct(Benefits_Calc[Benefit GUID])
        ),

        //Add a column containing a table of the count for every BenefitGUID
        Multiply = Table.AddColumn
        (
            BenefitGUID,
            "BaseAnalysisTimeLine",
            each Base_AnalysisTimeline
        ),

        //Expand the Tables in the New Column
        expandedTable = Table.ExpandTableColumn(Multiply, "BaseAnalysisTimeLine", {"Count"}, {"Count"}),
        //Rename GUID Column
        #"Renamed Columns" = Table.RenameColumns(expandedTable,{{"Column1", "Benefit GUID"}})
        
        in
            #"Renamed Columns",

    //Add Inflation Column to Table_Setup1
    Table_Setup2 = 
        let   
        //Get number of unique Benefit GUIDs
        noUniqueGUID = List.Count(List.Distinct(Benefits_Calc[Benefit GUID])),
        //Create temporary list of Inflation Rates
        tempList = Base_AnalysisTimeline[Compound Inflation Rate from Base],
        //Repeat the rows the same as unique GUIDs
        repeatList = List.Repeat(tempList, noUniqueGUID),
        tempTable = Table.FromColumns({repeatList}),
        //add index to that table:
        tempTableWithIndex = Table.AddIndexColumn(tempTable, "joinIndex", 0, 1),
        //add index to passed in table:
        targetTableWithIndex = Table.AddIndexColumn(Table_Setup1, "joinIndex", 0, 1),
        joinedTable = Table.Join(targetTableWithIndex , "joinIndex", tempTableWithIndex , "joinIndex", JoinSide.Left),
        //remove the index column from joined table we dont need it
        joinedTableWithoutIndex = Table.RemoveColumns(joinedTable, "joinIndex"),
        #"Renamed Columns" = Table.RenameColumns(joinedTableWithoutIndex,{{"Column1", "Compound Inflation Rate from Base"}})
        in
            #"Renamed Columns",

    //Add Discount Factor Column to Table_Setup2
    Table_Setup3 = 
        let   
        //Get number of unique Benefit GUIDs
        noUniqueGUID = List.Count(List.Distinct(Benefits_Calc[Benefit GUID])),
        //Create temporary list of Inflation Rates
        tempList = Base_AnalysisTimeline[Discount Factor],
        //Repeat the rows the same as unique GUIDs
        repeatList = List.Repeat(tempList, noUniqueGUID),
        tempTable = Table.FromColumns({repeatList}),
        //add index to that table:
        tempTableWithIndex = Table.AddIndexColumn(tempTable, "joinIndex", 0, 1),
        //add index to passed in table:
        targetTableWithIndex = Table.AddIndexColumn(Table_Setup2, "joinIndex", 0, 1),
        joinedTable = Table.Join(targetTableWithIndex , "joinIndex", tempTableWithIndex , "joinIndex", JoinSide.Left),
        //remove the index column from joined table we dont need it
        joinedTableWithoutIndex = Table.RemoveColumns(joinedTable, "joinIndex"),
        #"Renamed Columns" = Table.RenameColumns(joinedTableWithoutIndex,{{"Column1", "Discount Factor"}})
        in
            #"Renamed Columns"


in
    Table_Setup3

 

Not the most elegant solution I'm sure, but I'm happy with it for my first attempt.

 

Thanks again for all the help!

Hi Jimmy,

 

First, thanks very much for helping! Super useful!

 

I have a couple of questions where I don't quite understand how the code is working due to my limited knowledge:

 

1) For the Base_AnalysisTimeline table, why did you use the Table/JSON, etc. method to create the table rather than the usual "Source = Currentworkbook() etc."? Is there an advantage of doing it this way?

 

2) For "BenefitGUID = #table({"Benefit GUID"}, {{"U1B1"}, {"U1B2"}, {"U1B3"}})", I guess I could replace this with a query which calls List.Distinct from my existing Benefits_Calc table and remove the other columns to create a table from that? The number of GUIDs will vary over time, so I will need the table to update row numbers dynamically.

 

Thanks again!

Hi @Andyb431 ,

 

@Jimmy801 is right. You could just add the column "Benefit GUID" into another table and then expend it. I have created a simple sample using your file. Please have a try.

 

v-xuding-msft_0-1599554160902.png

v-xuding-msft_1-1599554264466.png

 

 

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

Hello @Andyb431 

 

this is only to be able to present here a solution, that everybody can analyse and understand. For sure you have to substitue this part with your data source ... if its currentworkbook, replace it with that

 

All the best

 

Jimmy

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors
Top Kudoed Authors