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
tthierry
Frequent Visitor

Append columns in one query in advanced editor

Good day

 

I am stuck. I have multiple excel files I want to combine and edit. The number of columns can vary from one file to another.

Each file looks like this:

Analysis
Blabla
For the month ended 31 December 2018
     
 Dec-18Nov-18Oct-18Sep-18
     
Income    
Revenue 1£163,219.50£254,610.46£311,841.82£161,455.40
Revenue 2£8,510.00£2,300.00£3,450.00£0.00
Revenue 3£6,670.00£5,558.41£0.00£18,898.41
Revenue 4£9,583.18£9,583.18£0.00£0.00
Total Income£187,982.68£272,052.05£315,291.82£180,353.81
     
Less Cost of Sales    
Direct Expenses£0.00£0.00£0.00£0.00
Finance Subsidy£0.00£0.00£0.00£0.00
Referral Fees£2,875.00£0.00£0.00£0.00
Total Cost of Sales£2,875.00£0.00£0.00£0.00
     
Gross Profit£185,107.68£272,052.05£315,291.82£180,353.81
     
Plus Other Income    
Interest Income£0.00£0.00£0.00£0.00
Total Other Income£0.00£0.00£0.00£0.00
     
Less Operating Expenses    
Rent£87,852.87£87,237.62£87,212.55£87,688.42
Salaries£894,026.33£890,222.36£909,003.47£987,168.28
Server Costs£20,671.48£9,919.21£0.00£10,725.36
Software Subscriptions£72,939.90£65,115.53£71,609.12£77,063.34
Staff Training£0.00£0.00£0.00£0.00
Staff Welfare£19,939.85£15,811.58£4,023.85£20,962.89
Stripe fees£311.19£565.80£350.52£260.13
Subcontractors£193,887.47£203,628.20£211,600.00£148,120.00
Subscriptions£9,200.00£0.00£1,656.00£0.00
Telephone & Internet£43,868.59£42,785.29£46,049.91£42,603.82
Travel - International£0.00£33,507.55£0.00£0.00
Travel - National£9,083.85£69.00£2,275.39£11,504.60
Total Operating Expenses£1,351,781.53£1,348,862.14£1,333,780.81£1,386,096.84
     
Net Profit-£1,166,673.85-£1,076,810.09-£1,018,488.99-£1,205,743.03

 

What I want to achieve is to combine these files into a single table which would look like this:

PnL CategoryValuePeriod
Revenue 1£163,219.50Dec-18
Revenue 2£8,510.00Dec-18
Revenue 3£6,670.00Dec-18
Revenue 4£9,583.18Dec-18
Total Income£187,982.68Dec-18
Direct Expenses£0.00Dec-18
Finance Subsidy£0.00Nov-18
Referral Fees£2,875.00Nov-18
Total Cost of Sales£2,875.00Nov-18
Gross Profit£185,107.68Nov-18
Interest Income£0.00Nov-18
Total Other Income£0.00Nov-18
Rent£87,852.87Nov-18
Salaries£894,026.33Nov-18
Server Costs£20,671.48Nov-18
Software Subscriptions£72,939.90Nov-18

 

Currently I am creating individual tables for each period using this code:

let
    Source = Table.SelectColumns(Analysis,{"Column1","Column2"}),
    #"Removed Top Rows" = Table.Skip(Source,4),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Top Rows",{{"Column1", "PnL Category"}, {"Column2", "Value"}}),
    #"GetValueFromCell" = Source{4} [Column2],
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Period", each GetValueFromCell),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([#"Period"] <> null)),
    #"Removed Top Rows1" = Table.Skip(#"Filtered Rows",2),
    #"Filtered Rows1" = Table.SelectRows(#"Removed Top Rows1", each ([PnLValue] <> null)),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows1",{{"Value", type number}, {"Period", type date}})
in
    #"Changed Type"

and I change "Column2" to "Column3" for the second period, to "Column4" for the 3rd period and so on.

 

Then I append the queries into one new query...

 

It works but it creates multiple tables and I was hoping to have only one query to transform the file in one table.
I hope it makes sense. Thank you very much.

T

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

See if you can use the technique here:

https://social.technet.microsoft.com/wiki/contents/articles/32915.power-bi-merge-query-with-m.aspx

 

I use that technique to combine multiple queries into one using Append here:

https://community.powerbi.com/t5/Community-Blog/Turning-quot-Web-by-Example-quot-into-Power-Query-Fu...

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

3 REPLIES 3
Greg_Deckler
Super User
Super User

See if you can use the technique here:

https://social.technet.microsoft.com/wiki/contents/articles/32915.power-bi-merge-query-with-m.aspx

 

I use that technique to combine multiple queries into one using Append here:

https://community.powerbi.com/t5/Community-Blog/Turning-quot-Web-by-Example-quot-into-Power-Query-Fu...

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler 

 

Firstly, THANK YOU! As I progressed massively with your resources.

 

Secondly, I am almost there but not quite yet. The Appended query function doesn't work and I get the error message:

 

Expression.Error: We cannot convert a value of type Table to type List.
Details:
Value=Table
Type=Type

 

My code is

let
    Source = Excel.Workbook(File.Contents("C:\Users\Blabla/Downloads\Profit and Loss[64].xls"), null, true),
    #"Profit and Loss1" = Source{[Name="Profit and Loss"]}[Data],
    #"Selected Columns"= Table.SelectColumns(#"Profit and Loss1",{"Column1","Column2"}),
    #"Removed Top Rows" = Table.Skip(#"Selected Columns",4),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Top Rows",{{"Column1", "PnLCategory"}, {"Column2", "PnLValue"}}),
    #"GetValueFromCell" = #"Renamed Columns"{0} [PnLValue],
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Period", each GetValueFromCell),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([#"Period"] <> null)),
    #"Removed Top Rows1" = Table.Skip(#"Filtered Rows",2),
    #"Filtered Rows1" = Table.SelectRows(#"Removed Top Rows1", each ([PnLValue] <> null)),
    #"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows1",{{"PnLValue", type number}, {"Period", type date}}),
    #"Extracted Table from Source1" = Table.SelectColumns(#"Profit and Loss1",{"Column1", "Column3"}),
    #"Removed Top Rows2" = Table.Skip(#"Extracted Table from Source1",4),
    #"Renamed Columns1" = Table.RenameColumns(#"Removed Top Rows2",{{"Column1", "PnLCategory"}, {"Column3", "PnLValue"}}),
    #"GetValueFromCell1" = #"Renamed Columns1"{0} [PnLValue],
    #"Added Custom1" = Table.AddColumn(#"Renamed Columns1", "Period", each GetValueFromCell1),
    #"Filtered Rows2" = Table.SelectRows(#"Added Custom1", each ([#"Period"] <> null)),
    #"Removed Top Rows3" = Table.Skip(#"Filtered Rows2",2),
    #"Filtered Rows3" = Table.SelectRows(#"Removed Top Rows3", each ([PnLValue] <> null)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows3",{{"PnLValue", type number}, {"Period", type date}}),
    #"Appended Query" = Table.Combine( #"Changed Type", #"Changed Type1")
    in
    #"Appended Query"

When I get to #"Changed Type", I have a table in the right format as below:

 

PnLCategoryPnLValuePeriod
Revenue145001/11/2018
Revenue250001/11/2018
Item123001/11/2018
Item22401/11/2018
Item35201/11/2018

 

And when I get to #"Changed Type1", I have another table in the right format as below:

 

PnLCategoryPnLValuePeriod
Revenue149201/12/2018
Revenue254201/12/2018
Item127201/12/2018
Item26601/12/2018
Item39401/12/2018

 

I want the #"Appended Query" step to give me:

 

PnLCategoryPnLValuePeriod
Revenue145001/11/2018
Revenue250001/11/2018
Item123001/11/2018
Item22401/11/2018
Item35201/11/2018
Revenue149201/12/2018
Revenue254201/12/2018
Item127201/12/2018
Item26601/12/2018
Item39401/12/2018

 

What have I missed please? Thanks in advance.

T

And what I missed was {}

#"Appended Query" = Table.Combine( {#"Changed Type", #"Changed Type1"})

Thanks a lot!!!

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.