Allocating the total value of a field to others based on Gross Revenue - powerbi

I am trying to allocate the total value of a channel to others based on Gross Revenue in Power Query. The data source is from SQL, a query that i made to recover the data from the channel and gross revenue.
In this case I am trying to allocate the total value of channel C (Total of C = 4) to A and B, based on its Gross Revenue.
This a dummy table, simplified from the thousands of rows and several columns that I have:
Channel
GR
A
5
A
1
B
10
B
4
C
1
C
3
The table from PowerQuery that I expect is as following:
Channel
GR
GR2
A
5
6
A
1
1.2
B
10
12
B
4
4.8
C
1
0
C
3
0
I can do it in Excel, as it follows - however, as I said before, I cannot use it because the data source comes from SQL and want to present several graphs and information about this table in PowerBI [Formula used in Excel, if it helps some way =IF(G2="C";0;H2+SUMIFS(H:H;G:G;"C")*(H2/(SUM($H$2:$H$18)-SUMIFS(H:H;G:G;"C"))))] :
Solving the problem using Excel
I do not understand M languange enough to replicate this formula and everything I tried it just results in Erros in each line.
Thanks in advance for your help.
TL;DR: Trying to allocate the total of the channel value to the other channel based on GR

Try this in powerquery
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Channel", type text}, {"GR", Int64.Type}}),
#"NotCFiltered Rows" = Table.SelectRows(#"Changed Type", each ([Channel] <> "C")),
#"CFiltered Rows" = Table.SelectRows(#"Changed Type", each ([Channel] = "C")),
#"Added Custom" = Table.AddColumn(#"NotCFiltered Rows", "GR2", each ( [GR]/ List.Sum(#"NotCFiltered Rows"[GR])) * List.Sum(#"CFiltered Rows"[GR]) + [GR] ,type number)
in #"Added Custom"

Related

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

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

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]
)

Sum product costs with same ID in Power Query

I have multiple rows with same ID but different products/prices. I would like to get this in Power Query (PowerBI) due to the automation.
Excel formula looks like this:
=SUMPRODUCT(IF($A$2:$A$7=A2;$B$2:$B$7))
And the table is:
A (ID)
B (price)
1
10
1
20
1
5
2
3
2
6
2
1
My goal is to get this:
C (Formula-price)
35
35
35
10
10
10
This step represent Column P & Q (Claims List) in this sheet: https://docs.google.com/spreadsheets/d/1J_nl2_Dgam7JDdyzX-urrO2AEXuCzhQkB1nogTLn2eA/edit#gid=0
In powerquery, right click and group on ID. Add a sum of one of the number columns and then below that, choose All rows as the operation.
After grouping use arrows atop the new column to expand the other columns
Sample, without the expansion
let Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}, {"Column3", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Column1"}, {{"Count", each List.Sum([Column2]), type nullable number}, {"data", each _, type table }})
in #"Grouped Rows"
So I am guessing a little bit but I think you would like to add the sum of B(price) grouped by A(id) to every row in your table. And you would like to use Powerquery for that.
First step would be to import the table into powerquery, then I duplicate the query and group it by ID. The third step is to merge these table by ID
Step 1
I took the following example data
Goto Data/From Table/Range and you should get
Step2 Right click on the query above and select Duplicate
I named the result sumTbl , grouped it by ID and with price as the column to sum
PS In the above step you can change to Advanced and add a second aggreagtion level and then expand. No need to merge!
The result looks like
Step 3 Now you merge both tables. I merged them as a new one
The result is
Now you only need to expand the column sumTbl
The result will be

Power BI - How to display only the values which appear more than once in a column

I have a table with multiple columns. One of these is 'EAN'. In this column there are supposed to be unique values. Unfortunatly this is not the case. Now I want to find all the values that appear more then once.
I tried the FILTER, EARLIER, COUNTROWS. Nothing gives the output I'm looking for.
Example:
Art A - 111
Art B - 123
Art C - 222
Art D - 222
Art E - 456
What I expect as output is just a table, column or chart where '222' appears.
Create your visual using the EAN field.
Then create a measure with the formula:
= COUNTROWS('Table')
and drag this measure into the filters pane, setting the condition to 'greater than 1'.
Here's one way just using Power Query and M Code:
let
//read in and type the data
Source = Excel.CurrentWorkbook(){[Name="Table8"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Art", type text}, {"Num", Int64.Type}}),
//selectd only those rows where there are at least two identical nums
filter = Table.SelectRows(#"Changed Type", each List.Count(List.Select(#"Changed Type"[Num], (li)=>li=[Num]))>1),
//sort the output to keep the duplicates together
#"Sorted Rows" = Table.Sort(filter,{{"Num", Order.Ascending}})
in
#"Sorted Rows"

PowerBI Show/Hide Column based on slicer value

power bi
Is it possible to hide/show column in table based on selected value in slicer? or instead of hide/show change value or column depends on selection
tnx
Data:
CG CC Amount
CG-A CC-A1 10
CG-A CC-A2 20
CG-A CC-A3 30
CG-B CG-B1 40
CG-B CG-B2 50
CG-B CG-B3 60
I have slicer that has 2 values: CG and CC
Slicer
CG
CC
result when CG is selected:
Code Sum of Amount
CG-A 60
CG-B 150
Grand Total 210
result when CC:
Code Sum of Amount
CC-A1 10
CC-A2 20
CC-A3 30
CG-B1 40
CG-B2 50
CG-B3 60
Grand Total 210
One way to achieve this is to unpivot your data.
In the Power Query Editor:
Open a blank query (New Source > Blank Query) and use your data table as the source.
Select the [CG] and [CC] columns and choose Unpivot Columns (Transform tab)
Move [amount] to end (optional) and rename the other two columns to [Slicer] and [Code])
Close and Aply
The result looks like this:
This is the script from the Advanced Editor:
let
Source = table,
#"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {"Amount"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Slicer"}, {"Value", "Code"}}),
#"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Slicer", "Code", "Amount"})
in
#"Reordered Columns"
In your report put 'tableUnpivoted'[Slicer] in a slicer visual. Then use a matrix and put 'tableUnpivoted'[Code] in Rows and 'tableUnpivoted'[Amount] (sum) in Values. Like so: