Table is Empty - Power Query

AllisterB

Board Regular
Joined
Feb 22, 2019
Messages
120
Office Version
  1. 365
Platform
  1. Windows
Hi

I want to test in a Power Query if the Excel Table (t_AccrTrialBalance) is empty. If so then display a message and stop the Query otherwise do the transformation. how do I do this ?

Thank You


My code at present is
let
Source = Excel.CurrentWorkbook(){[Name="t_AccrTrialBalance"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type any}, {"Column3", type text}, {"Column4", type text}, {"Column5", type any}, {"Column6", type any}, {"Column7", type any}}),
#"Removed Top Rows" = Table.Skip(#"Changed Type",5),
#"Transposed Table" = Table.Transpose(#"Removed Top Rows"),
#"Cleaned Text" = Table.TransformColumns(#"Transposed Table",{{"Column1", Text.Clean, type text}}),
#"Transposed Table1" = Table.Transpose(#"Cleaned Text"),
#"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),

in
#"Promoted Headers"
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
what do you mean by (t_AccrTrialBalance) is empty ?
an empty table has only headers but no rows

btw. use code tags for M , like [CODE] your code here [/CODE]
 
Last edited:
Upvote 0
what do you mean by (t_AccrTrialBalance) is empty ?
an empty table has only headers but no rows

btw. use code tags for M , like [CODE] your code here [/CODE]

HI

I run a macro as below and then ask theUser to past data into it. If the Query is run before the pasting t is done then the Query errors.

Hope the code below clarifies what I mean by an empty ytable.

Thanks

Allister

If Not ActiveCell.ListObject Is Nothing Then
On Error Resume Next
ActiveCell.ListObject.DataBodyRange.Delete
On Error GoTo 0

End If
 
Upvote 0
did you see this?
btw. use code tags for M , like [CODE] your code here [/CODE]

pribb.png


from post #3 it follows that you need vba not M code
 
Upvote 0
did you see this?
btw. use code tags for M , like [CODE] your code here [/CODE]

View attachment 17647

from post #3 it follows that you need vba not M code


Thanks sandy
The Code I included in post 3 was to show hothe Table became empty and to clarify what i meant by an empty able.

The problem I have is when a PQ runs - hence I need some M Code to correct the power Query.

i do appreciate your interest and wil try and put code in [/CODE] in thefuture

Thank you

Allister
 
Upvote 0
in short: Power Query doesn't support vba (imho fortunately)

so with M you can try Table.Profile to load your table or, if table is blank, show error or defined info, like
Sales OrderMakeModelSales OrderMakeModel
AAAHondaCRVAAAHondaCRV,Accord,Civic
AAAHondaAccordBBBToyotaYaris,Highlander
AAAHondaCivicCCCNissanAltima
BBBToyotaYaris
BBBToyotaHighlander
CCCNissanAltima

Sales OrderMakeModelQuery1
All values are null
 
Upvote 0
Hi Sandy

the VBA runs prior to the Query so the fact tat M doesn't support VBA shouldn't be a problem.

Can you supply the M Code to do the

IF table is Blank then Output a message
else perform the transformation steps


Thanks

Allister
 
Upvote 0
sure,
if Table.Profile(Table1)[Count] = Table.Profile(Table1)[NullCount] then "All values are null" else Table1
where Table1 is yours table then you need adapt your M to the last name=Table1
 
Upvote 0
Hi Sandy Like this ?
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="t_AccredoTrialBalance"]}[Content],
    #"Check If table Empty" = if Table.Profile("t_AccredoTrialBalance")[Count] = Table.Profile("t_AccredoTrialBalance")[NullCount] then "All values are null" else,
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type any}, {"Column3", type text}, {"Column4", type text}, {"Column5", type any}, {"Column6", type any}, {"Column7", type any}}),
    #"Removed Top Rows" = Table.Skip(#"Changed Type",5),
    #"Transposed Table" = Table.Transpose(#"Removed Top Rows"),
    #"Cleaned Text" = Table.TransformColumns(#"Transposed Table",{{"Column1", Text.Clean, type text}}),
    #"Transposed Table1" = Table.Transpose(#"Cleaned Text"),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"AccountNumber", type number}, {"Account Name", type text}, {"Account Class", type text}, {"Account Type", type text}, {"Inactive", type logical}, {"Year Balance", type number}, {"LY Year Balance", type number}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"AccountNumber", "Account Number"}, {"Year Balance", "TY Balance"}, {"LY Year Balance", "LY Balance"}}),
    #"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each Number.ToText([Account Number], "0000.000")),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Account Number"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Account Number"}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns1",{"Account Number", "Account Name", "Account Class", "Account Type", "Inactive", "TY Balance", "LY Balance"}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Reordered Columns", "Account Number", "Account Number - Copy"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "Account Number - Copy", Splitter.SplitTextByDelimiter(".", QuoteStyle.Csv), {"Account Number - Copy.1", "Account Number - Copy.2"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Account Number - Copy.1", Int64.Type}, {"Account Number - Copy.2", Int64.Type}}),
    #"Renamed Columns2" = Table.RenameColumns(#"Changed Type2",{{"Account Number - Copy.1", "Base"}, {"Account Number - Copy.2", "Subcode"}}),
    #"Reordered Columns1" = Table.ReorderColumns(#"Renamed Columns2",{"Account Number", "Base", "Subcode", "Account Name", "Account Class", "Account Type", "Inactive", "TY Balance", "LY Balance"}),
    #"Added Custom1" = Table.AddColumn(#"Reordered Columns1", "Custom", each Number.ToText([Subcode],"000")),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Subcode"}),
    #"Reordered Columns2" = Table.ReorderColumns(#"Removed Columns1",{"Account Number", "Base", "Custom", "Account Name", "Account Class", "Account Type", "Inactive", "TY Balance", "LY Balance"}),
    #"Changed Type3" = Table.TransformColumnTypes(#"Reordered Columns2",{{"Account Number", type text}, {"Base", type text}, {"Custom", type text}}),
    #"Renamed Columns3" = Table.RenameColumns(#"Changed Type3",{{"TY Balance", "This Year"}, {"LY Balance", "Last Year"}}),
    #"Removed Bottom Rows" = Table.RemoveLastN(#"Renamed Columns3",1)
 
in
    #"Removed Bottom Rows"
]
 
Upvote 0
if it works for you, you got an answer

I have no source data so I can't test it
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top