I am trying to dynamically change the Filter in an Excel Sage Power Query from a Named Range single cell D3 in below Image. D3 is a selection List from a Chart of Accounts in same Workbook.
Per the image above, the dynamic MonthNo works a charm for selecting the period/s, and I have used the same /similar code for the dynamic Account Number, but whilst it reads the AccountNo (Named Range) it gives the correct result, however the Table comes up empty as below Image.
My code is as follows:-
let
MonthNo= Text.From(Excel.CurrentWorkbook(){[Name="MonthNo"]}[Content]{0}[Column1]),
AccountNo= Text.From(Excel.CurrentWorkbook(){[Name="AccountNo"]}[Content]{0}[Column1]),
Source = Sql.Database("VM65\SAGE200", "UK_GamaAviationLtd", [Query="SELECT #(lf)NLNominalAccount.AccountNumber, NLNominalAccount.AccountCostCentre, NLNominalAccount.AccountDepartment, NLNominalAccount.AccountName,#(lf)NLPostedNominalTran.TransactionDate, NLPostedNominalTran.PostedDate, NLPostedNominalTran.GoodsValueInBaseCurrency, NLPostedNominalTran.Reference,#(lf)NLPostedNominalTran.Narrative, NLPostedNominalTran.UniqueReferenceNumber, NLPostedNominalTran.UserName, NLPostedNominalTran.UserNumber,#(lf)SYSAccountingPeriod.PeriodNumber#(lf)FROM NLPostedNominalTran, NLNominalAccount, SYSAccountingPeriod, SYSFinancialYear#(lf)WHERE NLPostedNominalTran.NLNominalAccountID = NLNominalAccount.NLNominalAccountID#(lf)AND NLPostedNominalTran.SYSAccountingPeriodID = SYSAccountingPeriod.SYSAccountingPeriodID#(lf)AND SYSAccountingPeriod.SYSFinancialYearID = SYSFinancialYear.SYSFinancialYearID#(lf)AND SYSAccountingPeriod.PeriodNumber <="&MonthNo&"#(lf)ORDER BY NLPostedNominalTran.UniqueReferenceNumber DESC"]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"TransactionDate", type date}, {"GoodsValueInBaseCurrency", Currency.Type}, {"PostedDate", type date}}),
Custom1 = Table.AddColumn(#"Changed Type", "Date Time Refresh", each DateTime.LocalNow() as datetime),
#"Changed Type1" = Table.TransformColumnTypes(Custom1,{{"Date Time Refresh", type datetime}}),
#"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"AccountNumber", "Account Number"}, {"AccountCostCentre", "Cost Centre"}, {"AccountDepartment", "Department"}, {"AccountName", "Account Name"}, {"TransactionDate", "Trans Date"}, {"PostedDate", "Posted Date"}, {"GoodsValueInBaseCurrency", "Posted Value"}, {"Reference", "Journal Ref"}, {"Narrative", "Journal Narrative"}, {"UniqueReferenceNumber", "URN"}, {"UserName", "User Name"}, {"UserNumber", "User Number"}, {"PeriodNumber", "Posted Period"}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns",{{"Posted Value", Currency.Type}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type2", "Trans Date", "Trans Date - Copy"),
#"Reordered Columns" = Table.ReorderColumns(#"Duplicated Column",{"Account Number", "Cost Centre", "Department", "Account Name", "Trans Date", "Posted Date", "Posted Value", "Journal Ref", "Journal Narrative", "URN", "User Name", "User Number", "Trans Date - Copy", "Posted Period", "Date Time Refresh"}),
#"Extracted Year" = Table.TransformColumns(#"Reordered Columns",{{"Trans Date - Copy", Date.Year, Int64.Type}}),
#"Renamed Columns1" = Table.RenameColumns(#"Extracted Year",{{"Trans Date - Copy", "Year"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns1", "Full Account No", each [Account Number]&[Cost Centre]&[Department]),
#"Reordered Columns1" = Table.ReorderColumns(#"Added Custom",{"Full Account No", "Account Number", "Cost Centre", "Department", "Account Name", "Trans Date", "Posted Date", "Posted Value", "Journal Ref", "Journal Narrative", "URN", "User Name", "User Number", "Year", "Posted Period", "Date Time Refresh"}),
#"Filtered Rows" = Table.SelectRows(#"Reordered Columns1", each ([Full Account No] = "&AccountNo&")),
#"Sorted Rows" = Table.Sort(#"Filtered Rows",{{"Year", Order.Ascending}, {"Posted Period", Order.Ascending}, {"Trans Date", Order.Ascending}})
in
#"Sorted Rows"
I have tried the above code where I have inserted the 2nd line AccountNo= Text.From(Excel.CurrentWorkbook(){[Name="AccountNo"]}[Content]{0}[Column1]),
, with the result of the empty table per the image, so it is not filtering dynamically. This code refers to the Named single cell Range in D3 (AccountNo) in the Workbook. I have tried various alternate iterations with no success. I have also tried changing the AccountNo= Text.From
to AccountNo= Number.From
which comes up with an Error, whilst with AccountNo= Text.From
when viewing AccountNo step I can see the result of the Account Number.
I have also changed the second last line of code from #"Filtered Rows" = Table.SelectRows(#"Reordered Columns1", each ([Full Account No] = "B20458")),
to #"Filtered Rows" = Table.SelectRows(#"Reordered Columns1", each ([Full Account No] = "&AccountNo&")),
. When I insert "B20458"
in place of "&AccountNo&"
, I get results and table for Account Number B20458, however I am wanting to change the Account Number dynamically from the Drop-down List in cell D3.
I also wish the added complication that if No Account Number is selected in D3 (Blank) then NO Filter is applied if possible.
Many thanks for your trouble.
Gary
Try
#"Filtered Rows" = Table.SelectRows(#"PriorStepNameGoesHere", each Text.From([Account Number]) = Text.From(Excel.CurrentWorkbook(){[Name="AccountNo"]}[Content]{0}[Column1]))