I am trying to do the same as someone has already asked on a Microsoft forum
I am trying to show Payment info in Power BI but mainly what months/Years Payments have been missed.
I have 3 Tables, Date Table, Payment Table and Client Table.
The Payment Table has the fields,[Client ID],[Payment Id],[Payment Date], Client Table has fields [Client Id] and [Client Name] etc, Date Table has is generated using the min and max [Payment Date] from the Payment Table.
Is this possible in Power Query or using DAX in Power Bi and if so how?
Thanks
You can try something like this
code for new table, missing
let Source = Date,
#"Added Custom9" = Table.AddColumn(Source, "Month", each Date.Month([Date])),
#"Added Custom10" = Table.AddColumn(#"Added Custom9", "Year", each Date.Year([Date])),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom10",{"Date"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Columns1", {"Month", "Year"}),
#"Added Custom" = Table.AddColumn( #"Removed Duplicates", "Client ID", each Clients[Client ID]),
NewDates = Table.ExpandListColumn(#"Added Custom", "Client ID"),
#"Added Custom12" = Table.AddColumn(Payments, "Month", each Date.Month([Payment Date])),
NewPayments = Table.AddColumn(#"Added Custom12", "Year", each Date.Year([Payment Date])),
#"Merged Queries" = Table.NestedJoin(NewDates, {"Month","Year","Client ID"}, NewPayments, {"Month","Year","Client ID"}, "Payments", JoinKind.LeftOuter),
#"Added Custom1" = Table.AddColumn(#"Merged Queries", "Paid?", each Table.RowCount([Payments])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([#"Paid?"] = 0)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Payments", "Paid?"}),
#"Merged Queries1" = Table.NestedJoin(#"Removed Columns", {"Client ID"}, Clients, {"Client ID"}, "Clients", JoinKind.LeftOuter),
#"Expanded Clients" = Table.ExpandTableColumn(#"Merged Queries1", "Clients", {"Client Name"}, {"Client Name"}),
#"Grouped Rows" = Table.Group(#"Expanded Clients", {"Month","Year"}, {
{"Missing Payments From", each Text.Combine(List.Transform([Client ID], Text.From), ","), type text},
{"Missing Payments From2", each Text.Combine(List.Transform([Client Name], Text.From), ","), type text}
})
in #"Grouped Rows"
remove the group step if you'd rather see the output in table format
Version 2 generates this:
//version2
let Source = Date,
#"Added Custom9" = Table.AddColumn(Source, "Month", each Date.Month([Date])),
#"Added Custom10" = Table.AddColumn(#"Added Custom9", "Year", each Date.Year([Date])),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom10",{"Date"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Columns1", {"Month", "Year"}),
#"Added Custom" = Table.AddColumn( #"Removed Duplicates", "Client ID", each Clients[Client ID]),
NewDates = Table.ExpandListColumn(#"Added Custom", "Client ID"),
#"Added Custom12" = Table.AddColumn(Payments, "Month", each Date.Month([Payment Date])),
NewPayments = Table.AddColumn(#"Added Custom12", "Year", each Date.Year([Payment Date])),
#"Merged Queries" = Table.NestedJoin(NewDates, {"Month","Year","Client ID"}, NewPayments, {"Month","Year","Client ID"}, "Payments", JoinKind.LeftOuter),
#"Added Custom1" = Table.AddColumn(#"Merged Queries", "Paid?", each Table.RowCount([Payments])),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([#"Paid?"] = 0)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Payments", "Paid?"}),
#"Merged Queries1" = Table.NestedJoin(#"Removed Columns", {"Client ID"}, Clients, {"Client ID"}, "Clients", JoinKind.LeftOuter),
#"Expanded Clients" = Table.ExpandTableColumn(#"Merged Queries1", "Clients", {"Client Name"}, {"Client Name"}),
#"Added Custom2" = Table.AddColumn(#"Expanded Clients", "MonthYear", each Text.From([Month])&"/"&Text.From([Year])),
#"Grouped Rows" = Table.Group(#"Added Custom2", {"Client ID","Client Name"}, {
{"Missing Payments", each Text.Combine(List.Transform([MonthYear], Text.From), ","), type text}
})
in #"Grouped Rows"
Related
Code
Function
let
FindTables = List.Buffer(TablesAndViews),
Source = Queries,
#"Removed other columns" = Table.SelectColumns(Source, {"Index", "Command Text inc SP"}),
#"Added custom" = Table.AddColumn(#"Removed other columns", "Custom", each Text_ExtractAll([Command Text inc SP], FindTables), type list),
#"Removed columns" = Table.RemoveColumns(#"Added custom", {"Command Text inc SP"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Removed columns", "Custom"),
#"Changed column type" = Table.TransformColumnTypes(#"Expanded Custom", {{"Custom", type text}}),
#"Renamed columns" = Table.RenameColumns(#"Changed column type", {"Custom", "Table Name"},MissingField.Ignore)
in
#"Renamed columns"
Original Code
#"Renamed columns" = Table.RenameColumns(#"Changed column type", {{"Custom", "Table Name"}})
Tried
#"Renamed columns" = Table.RenameColumns(#"Changed column type", {"Custom", "Table Name"},MissingField.Ignore)
Based on
https://learn.microsoft.com/en-us/powerquery-m/table-renamecolumns
I would like to know how to create the last column, Audit. To determine whether it is Included or Not Included the sum of any rows in "Average meeting per Day" within the same Employee/Day should match the "Actual Meeting" the employee attended.
I've taken this as an Example dataset -
Further, use Group By and Conditional column to have the Audit added.
Group by -
Conditional Column -
Result -
Code -
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s3PS0msVNJRMgRiAz1TMGkEJBOVYnVwSBtjShuBJQzhZBJYOqQ0tRhZuwlcHqg9=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Day = _t, Emp_ID = _t, #"Actual Meeting" = _t, #"Average Meeting/Day" = _t, Emp_Name = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Day", type text}, {"Emp_ID", Int64.Type}, {"Actual Meeting", type number}, {"Average Meeting/Day", type number}, {"Emp_Name", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Day", "Emp_ID"}, {{"Sum of avg", each List.Sum([#"Average Meeting/Day"]), type nullable number}, {"Actual Meeting", each List.Min([Actual Meeting]), type nullable number}}),
#"Added Conditional Column" = Table.AddColumn(#"Grouped Rows", "Audit", each if [Actual Meeting] = [Sum of avg] then "Included" else "Not Included")
in
#"Added Conditional Column"
Not sure this is going to help, but here goes. Note: not set up to work with duplicate values among the potential items to analyze. No idea if it will work with so many decimals, so that the sum of the decimals might be 0.0000001 off from total and thus not be recognized. Works for me with whole numbers
let
Process=(x as table) as table =>
// Bill Szysz 2017, all combinations of items in list, blows up with too many items to process due to large number of combinations
let ComboList=x[AMD],
find=x[AM]{0},
Source=Table.FromList(List.Transform(ComboList, each Text.From(_))),
AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1),
ReverseIndeks = Table.AddIndexColumn(AddIndex, "RevIdx", Table.RowCount(AddIndex), -1),
Lists = Table.AddColumn(ReverseIndeks, "lists", each
List.Repeat(
List.Combine({
List.Repeat({[Column1]}, Number.Power(2,[RevIdx]-1)),
List.Repeat( {null}, Number.Power(2,[RevIdx]-1))
})
, Number.Power(2, [Index]))
),
ResultTable = Table.FromColumns(Lists[lists]),
#"Replaced Value" = Table.ReplaceValue(ResultTable,null,"0",Replacer.ReplaceValue,Table.ColumnNames(ResultTable )),
#"Added Index" = Table.AddIndexColumn(#"Replaced Value", "Index", 0, 1, Int64.Type),
totals = Table.AddColumn(#"Added Index", "Custom", each List.Sum(List.Transform(Record.ToList( Table.SelectColumns(#"Added Index",Table.ColumnNames(ResultTable )){[Index]}) , each Number.From(_)))),
#"Filtered Rows" = Table.SelectRows(totals, each ([Custom] = find)),
#"Kept First Rows" = Table.FirstN(#"Filtered Rows",1),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Kept First Rows", {"Custom", "Index"}, "Attribute", "Value"),
#"Filtered Rows1" = Table.SelectRows(#"Unpivoted Other Columns", each ([Value] <> "0")),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows1",{"Value"}),
FoundList = Table.TransformColumnTypes(#"Removed Other Columns",{{"Value", type number}}),
#"Merged Queries" = Table.NestedJoin(x, {"AMD"}, FoundList, {"Value"}, "output1", JoinKind.LeftOuter),
#"Expanded output1" = Table.ExpandTableColumn(#"Merged Queries", "output1", {"Value"}, {"Value"}),
#"Calculated Absolute Value" = Table.TransformColumns(#"Expanded output1",{{"Value", each if _=null then "Not Included" else "Included", type text}})
in #"Calculated Absolute Value",
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Day", type text}, {"Employee_ID", Int64.Type}, {"AM", Int64.Type}, {"AMD", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Day", "Employee_ID"}, {{"data", each _, type table [Day=nullable text, Employee_ID=nullable number, AM=nullable number, AMD=nullable number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Process([data])),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Day", "Employee_ID", "AM", "AMD", "Value"}, {"Day", "Employee_ID", "AM", "AMD", "Value"})
in
#"Expanded Custom"
I want to transform a column in Power query. Only the transformation should be applied within the group based on a condition. This is my data.
Here in the above table, I just want to transform Office based column to all 1 if any Office-based column is set to 1 on the particular ID group. But all the Office based column value is 0 on the particular ID group, it should not transform the column.
My expected result would be,
It would be fine, If an additional column can have the transformed column.
try this
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Item", type text}, {"Home Based", Int64.Type}, {"Office Based", Int64.Type}, {"Amount", Int64.Type}}),
// find all IDs with 1 in Office Based
#"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Office Based] = 1)),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"ID"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
//merge that back in
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"ID"}, #"Removed Duplicates", {"ID"}, "Table2", JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"ID"}, {"ID.1"}),
// if there was a match convert to 1 otherwise take original number
#"Added Custom" = Table.AddColumn(#"Expanded Table2", "OfficeBased2", each try if Text.Length([ID.1])>0 then 1 else [Office Based] otherwise [Office Based]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Office Based", "ID.1"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"OfficeBased2", "OfficeBased"}})
in #"Renamed Columns"
or the more compact version:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source,"Custom",(i)=>Table.SelectRows(Source, each [ID]=i[ID]) [Office Based]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Office Based2", each if List.Contains([Custom],1) then 1 else [Office Based]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Custom", "Office Based"})
in #"Removed Columns"
The first method probably works best for large data sets
Here's another method:
Group by ID
Apply the Table.TransformColumns operation to each subtable
Then re-expand
let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Item", type text}, {"Home Based", Int64.Type}, {"Office Based", Int64.Type}, {"Amount", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {
{"Xform", (t)=>Table.TransformColumns(t, {
{"Office Based", each if List.ContainsAny(t[Office Based],{1}) then 1 else 0}}) ,
type table
[ID=nullable text, Item=nullable text, Home Based=nullable number, Office Based=nullable number, Amount=nullable number]}
}),
#"Expanded Xform" = Table.ExpandTableColumn(#"Grouped Rows", "Xform",
{"Item", "Home Based", "Office Based", "Amount"},
{"Item", "Home Based", "Office Based", "Amount"})
in
#"Expanded Xform"
Hi I'm trying to create a new table based on an existing table in power bi, how can I do this?
existing table
New table
New table group column can be exported from table3
Power Query solution
let
Source = Table.FromRows(
Json.Document(
Binary.Decompress(
Binary.FromText(
"i45WSlTSUUpMBhLJeToK+aW2KYlKsTrRSklAkZRMhHByKVg4GYvqWAA=",
BinaryEncoding.Base64
),
Compression.Deflate
)
),
let
_t = ((type nullable text) meta [Serialized.Text = true])
in
type table [user = _t, status = _t, dn = _t]
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
{{"user", type text}, {"status", type text}, {"dn", type text}}
),
#"Replaced Value" = Table.ReplaceValue(
#"Changed Type",
"cn, ou=",
"",
Replacer.ReplaceText,
{"dn"}
),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value", {"user"}),
#"Removed Other Columns" = Table.SelectColumns(#"Removed Columns", {"dn"}),
#"Removed Duplicates" = Table.Distinct(#"Removed Other Columns"),
#"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 1, 1, Int64.Type),
#"Merged Queries" = Table.NestedJoin(
#"Removed Columns",
{"dn"},
#"Added Index",
{"dn"},
"Added Index",
JoinKind.LeftOuter
),
#"Expanded Added Index" = Table.ExpandTableColumn(
#"Merged Queries",
"Added Index",
{"Index"},
{"Index"}
),
#"Pivoted Column" = Table.Pivot(
#"Expanded Added Index",
List.Distinct(#"Expanded Added Index"[status]),
"status",
"Index",
List.Count
),
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "Total", each [ac] + [di])
in
#"Added Custom"
Can anyone please advise on how to filter this column in Power Query so that it contains only the last date of each year?
So, this should contain only 3 rows:
31/12/2019
31/12/2020
31/03/2021
Try this
Add custom column to pull out the year
= Date.Year([EndDate])
Add custom column to pull out the max date for each matching year
= (i)=>List.Max(Table.SelectRows(#"Added Custom" , each [Year]=i[Year]) [EndDate])
Add custom column to check the two dates against each other
= if [EndDate]=[MaxDate] then "keep" else "remove"
Filter on that column
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"EndDate", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([EndDate])),
#"Added Custom2" = Table.AddColumn(#"Added Custom","MaxDate",(i)=>List.Max(Table.SelectRows(#"Added Custom" , each [Year]=i[Year]) [EndDate]), type date ),
#"Added Custom1" = Table.AddColumn(#"Added Custom2", "Custom", each if [EndDate]=[MaxDate] then "keep" else "remove"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Custom] = "keep"))
in #"Filtered Rows"
~ ~ ~
another way probably better for larger lists
Add custom column to pull out the year
= Date.Year([EndDate])
Group on year and take the Maximum of the EndDate Column
Merge that back to original data with left outer join and filter
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"EndDate", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([EndDate])),
#"Grouped Rows" = Table.Group(#"Added Custom", {"Year"}, {{"MaxDate", each List.Max([EndDate]), type date}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type",{"EndDate"}, #"Grouped Rows" ,{"MaxDate"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"MaxDate"}, {"MaxDate"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Table2", each ([MaxDate] <> null))
in #"Filtered Rows"
This is an older thread, but for those who are looking for the easiest solution, you can use :
= Table.SelectRows(#"Converted to Table", each [Date] = Date.EndOfYear( [Date] ) )
If you want to see the entire code in action, you can paste this in the advanced editor:
let
Source = List.Dates( #date( 2010, 1, 1), Duration.Days( #date( 2022, 12, 31) - #date( 2010, 1, 1 ) ) + 1 , #duration(1,0,0,0) ),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), type table[ Date = Date.Type ] , null, ExtraValues.Error),
#"Filtered Rows" = Table.SelectRows(#"Converted to Table", each [Date] = Date.EndOfYear( [Date] ))
in
#"Filtered Rows"
Hope that helps!
Rick de Groot
https://gorilla.bi