I have a table that looks like this:
Document PartNum Cost
A 1 5
A 1 5
A 2 3
A 2 3
B 1 1
B 1 1
B 3 4
B 3 4
I am trying to get the SUM of the cost for each part (counted once) on each document. So my new column would look like this:
Document PartNum Cost NewColumn
A 1 5 8
A 1 5 8
A 2 3 8
A 2 3 8
B 1 1 5
B 1 1 5
B 3 4 5
B 3 4 5
In R I would use a for loop, max and an append function of some kind. I am struggling to find a solution using DAX in PowerBI. Thank you in advance!
I have a solution using MQuery & DAX.
#"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"PartNum", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type1", "DocPart", each [Document]&[PartNum]),
#"Removed Duplicates" = Table.Distinct(#"Added Custom", {"DocPart"})
Step 1: Make PartNumber type text
Step 2: Create new custom column by concatenation of Document & PartNumber.
Step 3: Remove duplicates from New Custom Column.
Step 4: Now close N apply.
Step 5: create a new Measure using dax:
TotalCost = CALCULATE(SUM(uni[Cost]),ALL(uni[DocPart],uni[PartNumber],uni[Cost]))
Step 6: Drag required columns.
Note: Please refer screenshot for output data before applying code
Related
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"
I'm trying to replace a value with an string when a condition is met in two columns.
Column 1
Column 2
Column 3
A
1
Step 1 - Description for step one for A
B
2
Step 2 - Description for step one for B
C
1
Step 1 - Description for step one for C
C
3
Step 3 - Description for step one for C
A
1
Step 1 - Description for step one for A
B
3
Step 3 - Description for step one for B
I tried the code
= Table.ReplaceValue(
#"Renamed Columns",
each [Column 3],
each if [Column 1] = "A" and [Column 2] = "1" then "Step 1 - Description for step one for A" else [NULL])
where I was planning to iterate this code for all the combinations of column 1 and 2 (where each combination gets a certain string in column 3).
How would I be able to get this done? Thanks in advance!
I would definitely create and merge a table, but if you want to do it this way
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns", each [Column3], each if [Column2]=1 and [Column1]="A" then "Step 1 - Description for step one for A" else null ,Replacer.ReplaceText,{"Column3"})
note, you have to do else null not else [NULL] unless you really have that column name. else NULL would not work either since powerquery is case sensitive
Also check if you mean to use Column 1 or Column1 the difference being a space between the n and 1
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
For a report I am making. I have a date column. I extract the months from that column and then pivot the months.
So for example I might have a table like:
Part#
Month
Qty
1
1
10
1
2
10
1
3
10
1
4
10
1
5
10
1
6
10
Which when I pivot it becomes:
Part#
1
2
3
4
5
6
1
10
10
10
10
10
10
I then rename the columns along with doing a few other tasks like creating a column of maximums.
However, since the data I pull is only for 6-months worth. These columns names will change.
In the future it may be:
Part#
4
5
6
7
8
9
1
10
10
10
10
10
10
So a line like:
Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,{"1", "2", "3", "4", "5", "6"})
is going to create an error when the columns are 4,5,6,7,8,9. Is there a way to handle this issue?
You'll need to manually code a step to rename the columns after your pivot.
This M code pulls a table like the one in your example from the current workbook in Excel, then pivots as you describe, then renames the columns to Part#, 1st, 2nd, 3rd, 4th, 5th, and 6th. Yes, I said it renames to Part#, even though it doesn't really change that column name. Be sure to include Part# in the list coded in the step even though the Part# name isn't really changing, because you need to account for all column names extracted by Table.ColumnNames. Replace 1st, 2nd, 3rd, 4th, 5th, and 6th with your desired column names.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(Source, {{"Month", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(Source, {{"Month", type text}}, "en-US")[Month]), "Month", "Qty"),
#"Renamed Columns" = Table.RenameColumns(#"Pivoted Column", List.Zip({Table.ColumnNames(#"Pivoted Column"),{"Part#", "1st", "2nd", "3rd", "4th", "5th", "6th"}}))
in
#"Renamed Columns"
List.Zip interweaves two lists together. Here, it interweaves Part# from the original column names (those in the "Pivoted Column" step's table), then Part# from the list coded into #"Renamed Columns, then the next original column name (1 from your first example or 4 from your second example), then 1st from the column list coded into #"Renamed Columns, then the next original column name (2 from your first example or 5 from your second example), then 2nd from the column list coded into #"Renamed Columns, and so forth. This sets up the list needed for Table.RenameColumns.
You can use TableColumNames to get a list of column names, then you can transform that list. So for your example, you could use something like:
= Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,List.Skip(Table.ColumnNames(#"Pivoted Column"),1))
here is the instance:
Column 1 Column 2 Column 3
2.99 4 Price OK
1.99 4 Price below limit
12.99
5.99 6 Price OK
1.99 6 Price below limit
8.99 6 Price OK
So for Power BI context Column 2 is a custom column from power query, the goal is to set a threshold value for column 2 pack size, in this instance pack size of 4 needs to check for minimum price of $2.99 (higher is ok), below the price should be below limit, in instance of column 2 blanks (result should also be blank). In the instance of size 6 the minimum price to check for is 5.99.
Is there a decent way to go about this?
Let's do this in two steps. First, create a column MinPrice that defines your minimum prices.
if [Column 2] = 4 then 2.99
else if [Column 2] = 6 then 5.99
else null
Then create a column that compares the actual and the minimal
if [Column 1] = null or [Column 2] = null then null
else if [Column 1] < [MinPrice] then "Price below limit"
else "Price OK"
If you have a bunch of unique values in Column 2 that you need to create rules for, then create a reference table that you can merge onto your original table and expand the MinPrice column instead of the first step stated above.
Column2 MinPrice
-----------------
4 2.99
6 5.99
8 7.99
...