PowerBI Show/Hide Column based on slicer value - powerbi

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:

Related

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"

How to use list.range function dynamically in power query?

I have the table :
table
I want output of category column like containing rows of H,I,J,K as well when i use split column with delimeters.
How do I do the same dynamically?
How can i use list.range function with text.beforedelimiter and text.afterdelimiter to get rows of H,I,J,K as well in column 'Category'?
Sample output :
Category Month Cost
------------ --------- -------
A January 2887
A February 570
.
.
.
H September 602
H October 1204
H November 1011
H December 2699
I September 602
I October 1204
I November 1011
I December 2699
J September 602
J October 1204
J November 1011
J December 2699
K September 602
K October 1204
You can transform you category column into individual Lists.
Then expand the lists into rows.
For example:
//Transform first column into lists of categories
xForm1 = Table.TransformColumns(Previous Step, {"Category",
each if Text.Contains(_,"-") then
{Text.Split(_,"-"){0}..Text.Split(_,"-"){1}}
else Text.Split(_,",")}
Source
Lists
Expanded Lists
To get your desired output,
You will probably want to replace 0 with null
Select the first column
Unpivot other columns
Here is M code to xform your original data into your desired results:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
typed = Table.TransformColumnTypes(Source,{{"Category",Text.Type}} &
List.Transform(List.RemoveFirstN(Table.ColumnNames(Source)),
each {_,Int64.Type})),
//Transform first column into lists of categories
xForm1 = Table.TransformColumns(typed, {"Category",
each if Text.Contains(_,"-") then
{Text.Split(_,"-"){0}..Text.Split(_,"-"){1}}
else Text.Split(_,",")}),
//Replace 0's with nulls so they won't show up in the unpivot
#"Replaced Value" = Table.ReplaceValue(xForm1,0,null,Replacer.ReplaceValue,
Table.ColumnNames(xForm1)),
//Expand the List Column into rows
#"Expanded Category" = Table.ExpandListColumn(#"Replaced Value", "Category"),
//Unpivot the data (month) columns
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Category", {"Category"}, "Month", "Value")
in
#"Unpivoted Other Columns"
Source
Results
You can create a new Custom Column using this below code in Power Query Editor -
List.Range({Text.At([Category],0)..Text.At([Category],Text.Length([Category]) - 1)}, 0)
You will get an output as below-
Now you can apply Expand to new rows to the new list output and you will get results as below-
Now you can do your other calculations over the output.

In Power Query, how to replace values in multiple columns with values in another table?

I'm working with the American Community Survey in Power BI and want to replace values in every column with corresponding values in another table of response labels. For example, race is encoded as 1,2,3,4,5 but I want to replace it with Asian, Black, Native, etc according to the response labels provided.
Let's say I have the following table with three variables and peoples' responses:
variable1
variable2
variable3
1
2
3
2
3
2
3
1
1
3
3
2
2
2
3
I am provided with this table of response keys:
VarName
ResponseKey
ResponseLabel
variable1
1
blue
variable1
2
red
variable1
3
green
variable2
1
left
variable2
2
right
variable2
3
down
variable3
1
high
variable3
2
medium
variable3
3
low
What I want is those three variables with the peoples' responses as the 'ResponseLabel' like so:
variable1
variable2
variable3
blue
right
low
red
down
medium
green
left
high
green
down
medium
red
right
low
Usually I would go one by one and replace each variable by hand but I would rather have a root canal than do that for hundred plus variables with anywhere from 2-100 responses so I imagine there is a better way to do this.
So far, I've thought about making tables for each variable and merge with the original table but that sounds like a lot as well. Then I thought maybe I should write a function to iterate across the original table and recode each column one by one. I'm also thinking there might be a way to do this in M but I'm not sure.
Do you have any ideas?
Thanks!
Assume your Response Keys table is Table1 and loaded into Powerquery
For the top table, load into PowerQuery
Add column ... index column ...
Right click new index column and ... Unpivot other columns ...
Home .. Merge Queries ...
Set bottom table to Table1, and click to match Attribute with VarName and Value with ResponseKey, with left outer join
Click the arrows atop the new column and [x] expand ResponseLabel
Right click value column and Remove
Click select attribute column, then Transform .. pivot columns ... value = ResponseLabel and Advanced=dont aggregate
Right click index column and Remove
Done
Full code:
let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Index"}, "Attribute", "Value"),
#"Merged Queries" = Table.NestedJoin(#"Unpivoted Other Columns",{"Attribute", "Value"},Table1,{"VarName", "ResponseKey"},"Table1",JoinKind.LeftOuter),
#"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries", "Table1", {"ResponseLabel"}, {"ResponseLabel"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Table1",{"Value"}),
#"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Attribute]), "Attribute", "ResponseLabel"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
in #"Removed Columns1"

How to "convert" DAX code in M code: delete rows in calculated colum

I am working with Power Bi Desktop and i am trying to "convert" a DAX code into M code. I know that this is not really possible, because of M´s structure and intention. But i have seen other Programmers achieve their DAX code with some workarounds in M code.
To my Problem:
I need to find a way, to delete rows in Power Query editor, after i calculated, if that row is already in the table. Specifically: If the column [FIN] is identical AND the column [Laufleistung in km] is not higher than +=30, i count that row as a duplicate and therefore want to delete tis row. I need to achieve the following DAX Column, within Query Editor:
Count Doppelte = CALCULATE(
COUNT('Table A'[FIN]);
FILTER('Table A'; 'Table A'[FIN] = EARLIER('Table A'[FIN])) ;
FILTER('Table A'; 'Table A'[Laufleistung in km] <= EARLIER('Table A'[Laufleistung in km])+30) ;
FILTER('Table A'; 'Table A'[Laufleistung in km] >= EARLIER('Table A'[Laufleistung in km])
)
)
With this Column i count (so result in column is 2), whenever i have a duplicate row.
The result looks like this:
FIN
Laufleistung in km
Doppelte
ID001
500
1
ID004
200
1
ID001
529
2
ID004
205
2
ID001
700
1
So, i want to delete the rows with [Doppelte]=2.
I currently do not know how to achieve this calculation in Power Query Editor. DO you guys have any knowledge on how to do this?
Thank you,
Marcel :)
Edited answer. See if this works for you
For each [FIN] it finds the largest [Laufleistung in km] from all prior rows, and compares the value to the current [Laufleistung in km] to see if they are at least 30 different
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index","MaxD",(i)=>List.Max(Table.SelectRows(#"Added Index", each [FIN]=i[FIN] and [Index]<i[Index]) [Laufleistung in km]), type number ),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Doppelte", each try if [Laufleistung in km]-[MaxD] < 30 then 2 else 1 otherwise 1),
in #"Added Custom1"
Here I have attached the .pbix file for your reference as all steps are difficult to mention and explain.
Report File Here
In short-
I have duplicate your Table A first as Table A (2)
Then performed some transformation to the new table Table A (2)
Merge the new table Table A (2) to your Table A
Then performed some steps in Table A
Please feel free to ask question if anything is not clear. Here below is the final output-