Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

add dynamic row values from another table

Sorry if my post is duplicated. I had searched the issue and found nowhere. Let me know if i can do this way or not. If I can do then, can you please me on this.

 

I have 2 tables:
Table1- 

Table2- 

 
I want to create a new table using power query line below. Let me know if I can create by any other method also.

 

The values in each row should be dynamically calculated for each row*column. Thanks in advance.

  • Jimmy801's avatar
    Jimmy801
    6 years ago

    Hello Anonymous 

     

    change the AddedYear-step as follows

    	AddedYear = Table.AddColumn
    	(
    		ChangeType,
    		"Custom",
    		each Date.Year
    		(
    		    [Date]
    		)
    	),


    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

11 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello Anonymous 

     

    this involves quite a few transformation steps

    First Combine both tables, then get a list of column names that have to be unpivoted (criteria was contains "quantity"). Apply an Unpivot of quantity-columns, add a new column with the year, delete all not needed columns finally pivot the table again.

    Here the complete solutuion

    let
    	Table1 =
    	let
    		Source = #table 
    		(
    			{"Region","Territory","Date","Brand","Quantity1","Quantity2"},
    			{ {"Central","A","Jan 2019","Nike","14","10"}, {"West","D","Feb 2019","Nike","5","54"} } 
    		)
    	 in 
    		Source,
    	Table2 =
    		let
    			Source = #table 
    			(
    				{"Region","Territory","Date","Brand","Quantity3"},
    				{ {"Central","A","Sep 2020","Nike","12"}, {"North East","B","Feb 2019","Nike","11"} } 
    			)
    		 in 
    			Source,
    	Combine = Table.Combine
    	(
    		{Table1,Table2}
    	),
    
    	GetQuantityList = List.Select
    	(
    		Table.ColumnNames
    		(
    			Combine
    		),
    		each Text.Contains
    		(
    			Text.Lower
    			(
    				_
    			),
    			"quantity"
    		)
    	),
    	Unpivot = Table.Unpivot 
    	(
    		Combine,
    		GetQuantityList,
    		"Quantity",
    		"Value" 
    	),
    	ChangeType = Table.TransformColumnTypes
    	(
    		Unpivot,
    		{{"Value", type number}}
    	),
    	AddedYear = Table.AddColumn
    	(
    		ChangeType,
    		"Custom",
    		each Date.Year
    		(
    			Date.FromText
    			(
    				[Date]
    			)
    		)
    	),
    	RemoveOtherColumns = Table.SelectColumns
    	(
    		AddedYear,
    		{"Custom", "Value", "Quantity"}
    	),
    	PivotColumn = Table.Pivot
    	(
    		Table.TransformColumnTypes
    		(
    			RemoveOtherColumns,
    			{{"Custom", type text}},
    			"de-DE"
    		),
    		List.Distinct
    		(
    			Table.TransformColumnTypes
    			(
    				RemoveOtherColumns,
    				{{"Custom", type text}},
    				"de-DE"
    			)
    		[Custom]
    		),
    		"Custom",
    		"Value",
    		List.Sum
    	)
     in 
    	PivotColumn

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query.

    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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Jimmy801 ,

      I am trying to run the query and get the below error.

       

      • Jimmy801's avatar
        Jimmy801
        Community Champion

        Hello Anonymous 

         

        change the AddedYear-step as follows

        	AddedYear = Table.AddColumn
        	(
        		ChangeType,
        		"Custom",
        		each Date.Year
        		(
        		    [Date]
        		)
        	),


        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