In trying to make a Vlookup on PowerQuery that also makes a sum of the multiple values fond. I have 2 tables on my Power BI that are conected by the Report Number as showed below. I need to create a new column on table B that gets the sum of cost at Table A according to their report numbers.
At Power Query I have created a new Column on Table B using the following code:
After that I was planning to simply create a new column summing the list result, but my list is Empty and I can't realize why. Can anyone help me understand why I can't get the results?
I can't do this using DAX, it should be in M
One way to add the column into TableB is:
= (i)=>List.Sum(Table.SelectRows(TableA, each [Report Num]=i[Report Num]) [Cost])
Another way is to Group TableA and merge it in. I tend to think this is a faster method for larger tables
let Source = Excel.CurrentWorkbook(){[Name="TableB"]}[Content],
#"Grouped Rows" = Table.Group(TableA, {"Report Num"}, {{"Cost", each List.Sum([Cost]), type number}}),
#"Merged Queries" = Table.NestedJoin(Source, {"Report Num"}, #"Grouped Rows", {"Report Num"}, "Table1", JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"Cost"}, {"Cost"})
in #"Expanded Table1"
of course, if those are the only two columns in TableB, you could just create the whole table in one go
let Source = Table.Group(TableA, {"Report Num"}, {{"Cost", each List.Sum([Cost]), type number}})
in Source
Related
I am new to Power BI Desktop and Power BI Query. The left part is the data, and the right part is my desired output in Power BI. I tried to use the Matrix Report Visual to format my data like the table on the right, but it only allowed me to select the Product column in the row. I hope to get the desired output without creating separate tables for each quarter.
You could bring your data into powerquery, and use this code to transform the data. You'd have to modify it to show your actual table name (Table1) and the name of the columns as appropriate. It basically groups on index/product to find the most recent target sale, expands the data back, then pivots to get the sideways view
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"index", "product"}, {
{"data", each _, type table },
{"most_recent_target_sale", each Table.Sort(_,{{"time", Order.Descending}}){0}[target_sale], type number}
}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"time", "sale"}, {"time", "sale"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded data",{{"index", type number}}),
#"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[time]), "time", "sale", List.Sum)
in #"Pivoted Column"
I have a matrix table in Power BI where the lowest heirarchy has 2 users with same product but for their manager, it needs to be only counted once. How can I do that in the matrix table?
When I was pulling the heirarchy from one table and sales from another, Power Bi was doing it on it's own but when sales is in the same table as the user heirarchy, it is simply taking a sum of all the sales when it should only sum once for cases when product is repeated for multiple users for the same manager.
As seen in the image, manager's total should be 300 but Power BI sums it up to 400. How can I make sure that manager's total is taken as 300? I'd really appreciate any help. Thank you
Simply put, you should remove the duplicate items related to manager "A" in the "Product" column. In the real scenario, you need to filter this way for each manager.
You can do this within Power Query:
(notice the table name 'SalesTable')
let
Source = Excel.CurrentWorkbook(){[Name="SalesTable"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each [Manager] = "A"),
#"Changed Type" = Table.TransformColumnTypes(#"Filtered Rows",{{"Manager", type text}, {"User", type text}, {"Product", Int64.Type}, {"Sales", Int64.Type}}),
#"Duplicate Removed" = Table.Distinct(#"Changed Type", {"Product"}),
Sales = #"Duplicate Removed"[Sales],
CustomSUM = List.Sum(Sales)
in
CustomSUM
Table 1:
Table 2:
Result:
I have tried using Fuzzy matching but it's not giving me the results.
Please help!
See if this works for you in powerquery
Find instances of items from Table2 within Table1
Code for Table1, after having created Table2:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
RelativeMerge = Table.AddColumn(Source, "RelativeJoin",
(x) => Text.Combine(Table.SelectRows(Table2, each Text.Contains(x[Column1],[Column1], Comparer.OrdinalIgnoreCase))[Column1],"::")),
y=List.Transform({1 .. List.Max(List.Transform(RelativeMerge[RelativeJoin], each List.Count(Text.Split(_,"::"))))}, each "match_"& Text.From(_)),
#"Split Column" = Table.SplitColumn(RelativeMerge, "RelativeJoin", Splitter.SplitTextByDelimiter("::", QuoteStyle.Csv), y)
in #"Split Column"
I am having a problem in PowerBi, where I want to get results for Actual vs Plan. I have 3 tables , Book Sales Table, Plan table and Titles table. I would like the result as in Desired Output table.
Book Sales
Titles
Plan
Desired Output
You can do this in Power Query M Code
Join the Sales and Titles tables to extract the publishing unit for each book type
Add a column representing the start of each month for each sales number
Group by BOM and Unit and aggregage with Sum in case you have multiple sales/unit in a month
Join this last table with the Plan table based on Unit and Date
M Code
let
//Join the Sales and Titles to get the Publishing units
Source = Table.NestedJoin(#"Book Sales", {"Book ID"}, Titles, {"Book Id"}, "Titles", JoinKind.LeftOuter),
#"Expanded Titles" = Table.ExpandTableColumn(Source, "Titles", {"Publishing Unit"}, {"Publishing Unit"}),
//add start of month column to merge with Plan
bom = Table.AddColumn(#"Expanded Titles", "BOM", each Date.StartOfMonth([Invoice Date]),type date),
//Group by unit and bom columns in case you happen to have multiple sales in the same month
#"Grouped Rows" = Table.Group(bom, {"Publishing Unit", "BOM"}, {{"Sales amount", each List.Sum([Sales amount]), type number}}),
//Join with Plan using bom and date as the key
salesVplan = Table.NestedJoin(Plan,{"Business Unit","Date"}, #"Grouped Rows", {"Publishing Unit","BOM"},"joined",JoinKind.FullOuter),
#"Expanded joined" = Table.ExpandTableColumn(salesVplan, "joined", {"Sales amount"}, {"Sales amount"})
in
#"Expanded joined"
This could be done in several different ways, but I would recommend doing this in SQL personally. This means that you can:
Select from all the tables you want to show, into the same table. Lets call it Products.
Then you join in the values that you want on that.
This way, there is no dax calculation that takes time, but rather just something that takes time while you refresh the dataset itself.
I'm trying to organize my data and extract only the necessary information for a Power BI dashboard.
I want to create another table, and in this table I will manipulate some data and display the occurrence of a particular set of characters in the column in Table 1 and put the number of occurrences in table 2.
How can I do that? I tried using "Conditional Column" but I can only use data within the table I'm working on.
I tried creating a new table with conditional column, but I can only manipulate data within the table I'm working on, and can't import data from other tables.
Does this do what you need?
If not, please explain better with a sample of what you are trying to do
let Table1 = Table.Buffer(#table({"Key2"},{{"Auto Cat"},{"Bus Car"},{"Car"}, {"Fog"}})),
Table2 = Table.Buffer(#table({"Key1"},{{"Auto"},{"Bus"},{"Cat"},{"Dog"}})),
ZMerge = Table.AddColumn(Table1, "RelativeJoin", (zz) => Table.SelectRows(Table2, each Text.Contains(zz[Key2],[Key1], Comparer.OrdinalIgnoreCase))),
#"Expanded RelativeJoin" = Table2 & Table.ExpandTableColumn(ZMerge, "RelativeJoin", {"Key1"}, {"Key1"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded RelativeJoin", each ([Key1] <> null)),
#"Grouped Rows" = Table.Group(#"Filtered Rows", {"Key1"}, {{"Count", each Table.RowCount(_)-1, Int64.Type}})
in #"Grouped Rows"