In a matrix table in Power BI, how to make sure that the table doesn't calculate subtotals and totals for duplicates? - powerbi

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

Related

Dynamically changing columns in Power BI

I have an API data source I am refreshing daily to gather power bi activity. Each day, the data returns a different amount of columns, so it might have 60 one day and 80 (+20) additional another day.
When I try to refresh the dataset in the Power BI Service, it naturally fails and states that the new columns cannot be found in the row set.
I have explored many options such as creating a combine table, however I do not know all the names of the columns that could come in each day so this failed because it was very static. Does anyone know of a way to dynamically handle these daily changes?
Many thanks
The only way to refresh a data source that has changing schema is to unpivot that table and bring it into your model as key/value pairs.
It depends on what you want to do.
If you're just trying to change all extra columns from type any to type number, you can try something like
let
Source = #"table with extra columns",
OtherColumnNames = List.RemoveItems(
Table.ColumnNames(Source),
#"List of known column names"
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
List.Transform(
OtherColumnNames,
each {_, type number}
)
)
in
#"Changed Type"
or, if that's something you will be doing to multiple tables you could turn it into a function, like a query with a name of "fTransformOtherColumnTypes" with the following code.
(
#"List of known column names" as list,
#"table with extra columns" as table,
Type as type
) =>
let
Source = #"table with extra columns",
OtherColumnNames = List.RemoveItems(
Table.ColumnNames(Source),
#"List of known column names"
),
#"Changed Type" = Table.TransformColumnTypes(
Source,
List.Transform(
OtherColumnNames,
each {_, Type}
)
)
in
#"Changed Type"
and then your other queries can use it, e.g. fTransformOtherColumnTypes({"name","color","org", "alias"}, #"your source data", type number)

How to calculate average weekly active visitor using DAX in PowerBI

*Take note that Visit 1 means the first visit of the day while Visit 2 means the 2nd visit for the day.
Hello everyone,
I'm trying to calculate average weekly active visitors in PowerBI using DAX.
First, I need to calculate how many unique visitors visit the shop in one week (Mon-Sun). Based on the calendar, 5/10/2022 - 5/13/2022 will be categorized under one week, and 5/16/2022 - 5/18/2022 is another week. So for the week from 5/10/2022 - 5/13/2022, there are 9 unique visitor and for the week from 5/16/2022 - 5/18/2022, there are 3 unique visitors.
Once I found out the unique visitor for respective weeks, then I can get the average weekly active visitor by:
(9+3)/2 = 6 visitors
Hence, the answer for the example in the screenshot will be 6 visitors. The output will be used in card visualization.
Struggling to find the best way to do this, any help or advise will be greatly appreciated!
Sample Data:
https://docs.google.com/spreadsheets/d/10TsJUy-Lkdpb9Eeh5itx-XE59hytC_i5I_6UeLLHS84/edit#gid=0
I don't know enough about DAX (or Power BI) to devise a DAX only solution.
However, you can calculate the Distinct Visitors per week using Power Query M Code (Home=>Transform in Power BI), and then create a Measure to show the Average visitors per week on the card.
Power Query M Code
Edit: Change from computing weeknumber to computing start of week to avoid problems if dates span more than a year, per comment below by #weizer
let
//change next line to whatever your actual source is
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
//Don't need this column for the output
#"Removed Columns" = Table.RemoveColumns(Source,{"Visit"}),
//Set data types
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Visitor ID", Int64.Type}, {"Date", type date}}),
//add custom column referenced to "StartOfWeek" so we can group by week
#"Added Custom" = Table.AddColumn(#"Changed Type", "WeekNumber", each Date.StartOfWeek([Date],Day.Monday), type date),
//Group by week number
// Then aggregate by week range and distinct visitor count
#"Grouped Rows" = Table.Group(#"Added Custom", {"WeekNumber"}, {
{"Date Range", each Date.ToText(List.Min([Date])) & " - " & Date.ToText(List.Max([Date])), type text},
{"Distinct Visitors", each List.Count(List.Distinct([Visitor ID])), Int64.Type}
}),
//Remove unneeded Weeknumber column
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"WeekNumber"})
in
#"Removed Columns1"
The Measure for the card will be just a simple Average function:
Distinct Visitors per Week = Average(yourTableName[Distinct Visitors])
VAR days =
SUMMARIZE(
'Sheet1'
,'Sheet1'[Date]
)
VAR Calend=
ADDCOLUMNS(
days
,"week",WEEKNUM('Sheet1'[Date])
)
VAR weeks=
SUMMARIZE(
Calend
,[week]
,"qty",COUNTROWS(VALUES('Sheet1'[Visitor ID]))
)
RETURN
AVERAGEX(
weeks
,[qty]
)

Custom grouped column and removing identical rows

I have raw data like this:
There's other columns not shown but disease site is the only one that will change within a study.
And in power Bi, ultimately in the report I want to show a table to colleagues that has just one row per study. Obviously given the unique disease sites, I need to group those first, and I need some help doing so. What I'd like to show colleagues is a table like this:
Where if there were multiple disease sites associated with a "study" then they are clumped as "multi". I figure to do so it'll mean creating a custom disease site column with 'multi' in it and then filter to one row per study, but I'm having trouble with the details.
Do I do that in power query? Should I do it in power bi after the query is imported? Any help would be appreciated, thank you!
Load your data into Powerquery or similar
Click select the Study and Primary_Investigatory columns, right click, group by and choose operation All Rows
Change the ending of the group in the formula window (or in Home .. advanced editor) from
{"Primary_Investigatory", "Study"}, {{"data", each _, ... })
to
{"Primary_Investigatory", "Study"}, {{"data", each if Table.RowCount(_) = 1 then [Disease_Site]{0} else "Multi"}})
sample full code for example image:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Primary_Investigatory", type text}, {"Study", type text}, {"Disease_Site", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Primary_Investigatory", "Study"}, {{"data", each if Table.RowCount(_) = 1 then [Disease_Site]{0} else "Multi"}})
in #"Grouped Rows"

Actual Vs Plan in PowerBI

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.

Vlookup in M language and sum values

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