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

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"

Related

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

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"

Power BI Store product of previous row subtracting from another column into the next row

Current Setup
CntrlRunTot is a hardcoded column of values. I'm trying to make a calculated column that subtracts its previous row from the demand qty that corresponds to its row and have that product stored in the row below. Ex. 188535 - 2976 = 185559 which is then stored a row beneath the 188535 row. Right now my Running Total calculated column depends on every value of CntrlRunTot but I need it to just depend on that first number 188535 and then be able to subtract Demand Qty and store the following values within its consecutive rows.
You can do that by editing the Query (Home=>Transform to get to the query editor from Power BI).
I use the List.Generate function to generate a list of the Running Totals; then combine that column with the rest of the table
M Code
let
//Change next two lines to whatever your data source really is
Source = Excel.CurrentWorkbook(){[Name="Table11"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Demand Qty", Int64.Type}, {"ContrlRunTot", Int64.Type}, {"Index", Int64.Type}}),
//Add running total column
// Change Table.ToColumns argument to the correct reference
#"Add Running Total" = Table.FromColumns(
Table.ToColumns(#"Changed Type") &
{List.Generate(
()=>[rt=#"Changed Type"[ContrlRunTot]{0}, idx=0],
each [idx] < Table.RowCount(#"Changed Type"),
each [rt = [rt] - #"Changed Type"[Demand Qty]{[idx]+1}, idx=[idx]+1],
each [rt])},
type table[Demand Qty=Int64.Type, ContrlRunTot=Int64.Type, Index=Int64.Type, Running Total=Int64.Type])
in
#"Add Running Total"

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

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"

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