How to refer to a column value of a Table in measure - powerbi

I have a Table in PowerBI, and it has a column which I added by New Column in PowerBI and I have a DAX function to compute the value for each row.
Then I create a new Measure for the same table. I need to create a Measure instead of Column because I need to use SelectedValue.
But for the new measure , I get error saying The value for 'MyColumn' cannot be determined. Either the column does not exist or there is no current row for this column.
Can you please tell me how can I solve this issue>?
I have a slider using the Type of Table 1, so that it has 3 selections "A", "B", "C"
Table 1
Type
A
B
C
And I have another Table 2 in m PowerBI
Table 2
Name
A Count
B Count
C Count
Paul
1
1
Jane
1
2
John
3
I want to build a measure/relationship such that if 'A' of Slider is selected, any row of Table 2 with "A count" has values will be displayed, otherwise, get it hidden.
So in this case,
when 'A' is selected, 'Paul' gets displayed,
when 'B' is selected, 'Jane', 'John' gets displayed,
when 'C' is selected, 'Paul', 'Jane' gets displayed.

Answer to the new question added afterwards:
For DAX calculations you want to avoid “Excel-style” crosstabs like your Table 2 at all costs. The by far easiest way to unpivot this table, would be by using PowerQuery. But if for some reason this is not possible, you can also do it "manually" in DAX:
Stacked 2 =
FILTER(
UNION(
SELECTCOLUMNS(
'Table 2',
"Name", 'Table 2'[Name],
"Attribute", "A Count",
"Value", 'Table 2'[A Count]),
SELECTCOLUMNS(
'Table 2',
"Name", 'Table 2'[Name],
"Attribute", "B Count",
"Value", 'Table 2'[B Count]),
SELECTCOLUMNS(
'Table 2',
"Name", 'Table 2'[Name],
"Attribute", "C Count",
"Value", 'Table 2'[C Count])
),
NOT ISBLANK([Value])
)
The result of this Calculated Table will look like this:
Now you need to extend your "Table 1" by a corresponding Calculated Column:
Attribute = 'Table 1'[Type] & " Count"
All that is left is to create a relationship between the 2 Attribute columns and you're done:
Put 'Table 1'[Type] in a slicer and 'Stacked 2'[Name'] in a table visual and everything will work like a charm:

What you experience is the row context. This only exists in a calculated column, not in a measure. The idea of a measure is to aggregate values in "vertical direction", in a column, whereas a calculated column does calculations in "horizontal direction", row by row.
Bottom line: In measures you always have to use aggregator functions, like SUM(), COUNT(), MIN() around a column reference, e.g.
Measure = SUM('Table'[Column])
But if you still need a row context in a measure, you can use an iterator function like SUMX(), which iterates over a table row by row, thereby establishing the row context, e.g.
Measure =
SUMX(
'Table',
'Table'[Column A] * 'Table'[Column B]
)
which will create a sum product of 'Table'[Column A] and 'Table'[Column B].
Note that if your table happens to have an unique ID column, you can filter your aggregating measure by this column and you'll get values for every row, effectively "removing" the aggregation.

Related

PowerBI - How to count same values from multiple columns

This is my first question here.
This time I want to count values that appear in different columns. Each one corresponds to the values from a row, but there is only 1 column that they have in common (not shown in the picture). I need a measure to show a count of each word described in those cells.
For example, in this case (please ignore blanks, it's a test), the measure should give a count for the word 'Desodorante' as 3, 'Cabello' as 2, and the rest 1. These words are pre defined and there are no random values accepted.
I may say that I want to state each of these words as a kind of category. I would like to make a slicer out of them too.
example
I believe a workaround is to create a calculated table, stating as columns each of these values and allocating a count of each value from these 4 columns?
A Power Query solution to this problem has been posted here: PowerBI - Count instances of string in multiple columns
A DAX solution to this problem would be to create a new calculated table by appending the 4 columns using UNION() and SELECTEDCOLUMN() and then getting the count via SUMARIZE():
The 2 calculated Tables:
Mesa =
FILTER(
UNION(
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto2]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto3]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto4]
)
),
[Producto] <> BLANK()
)
Producto Count =
SUMMARIZE(
'Mesa',
'Mesa'[Producto],
"Count", COUNT('Mesa'[Producto])
)

Power BI how to add a new row based on condition after group

I have a table like this:
enter image description here
I want to create a default value for each group(each month should have a default amount 0), I know we should do group function first, but I do not know want to do next, very appreciate who give me help
I'm not sure exactly where you are going with your request. I understand it as: Adding a row for the first day of each month with a default value of zero.
This is not easy DAX. It's a bit complicated, so feel free to provide more info about your need, there may be another way more simple way.
The first step is to generate a table of the first day of each month. After a quick search, I've found a way in another answer and I was able to modify it a bit.
TABLE2 =
SELECTCOLUMNS(
FILTER(
ADDCOLUMNS(
CALENDAR(
DATE(2022, 01, 01),
DATE(2022, 12, 01)
),
"IsFDOM",
IF(
DAY([Date]) = 1,
TRUE(),
FALSE()
)
),
[IsFDOM] = TRUE()
),
"DATA",
[Date],
"AMOUNT",
0
)
What it does:
It generate a calendar between two dates I've arbitrary set : January 1st to Decembre 1st.
It adds a columns in this generated table IsFDOM (Is First Day Of Month) that return True is it is.
This table is then filtered to keep only the rows where IsFDOM is True.
It is wrapped in a SELECTCOLUMNS to keep and rename the columns you want and to match your existing table.
Next, you want to merge your existing table with this newly generated table. The DAX function to do it is UNION and requires both table to have the same format - i.e same number of columns.
TABLENEW =
UNION(
TABLE1,
FILTER(
TABLE2,
NOT TABLE2[DATA] IN VALUES(TABLE1[DATA])
)
)
I've named you existing table TABLE1 in this example.
The FILTER is used to not merge rows (dates) that already exist in TABLE1.
And, to make things even nicer. We can bypass the creation of TABLE2 by embedding it in the table expression of TABLENEW :
TABLENEW =
VAR MaxDate =
MAX(TABLE1[DATA])
VAR TMP_Calendar =
FILTER(
SELECTCOLUMNS(
SUMMARIZE(
FILTER(
ADDCOLUMNS(
CALENDAR(
DATE(2022, 01, 01),
DATE(2022, 12, 01)
),
"IsFDOM",
IF(
DAY([Date]) = 1,
TRUE(),
FALSE()
)
),
[IsFDOM] = TRUE()
),
[Date],
"default",
0
),
"DATA",
[Date],
"AMOUNT",
[default]
),
NOT [DATA] IN VALUES(TABLE1[DATA]) &&
[DATA] <= MaxDate
)
RETURN
UNION(
TABLE1,
TMP_Calendar
)
The calendar formerly known as TABLE2 is now created in a variable TMP_Calendar.
To make thing nicer, MaxDate calculate the max date of your existing table and only merge for existing date.

power bi lookup and match value from another table

how to create a DAX formula to return the 'Progress' value in Table A from Table B matching the Table A [Order Status] to Table B [Status]
Do I need to set relationship between Table A [Order Status] and Table B [Status]. I tried to set relationship Table B to Table A One to many(1:*) but got error saying the cardinality selected isn't valid. please help
It should work as you describe, with a relationship between 'Table A'[Order Status] and 'Table B'[Status].
But if you want to make a measure you can use USERELATIONSHIP as part of CALCULATE, for example:
Progress Measure =
CALCULATE(
MAX( 'Table B'[Progress] ),
USERELATIONSHIP(
'Table A'[Order Status]
'Table B'[Status]
)
)
Alternatively you can make the join in Power Query.

Filter Power BI visualisation based on multiple column values

I'm very new to Power BI so apologies if this is an obvious question...
I have a visualisation of a table on my report and I want to filter only this visualisation based on 1 columns of 'Table A', 'Category', and 'Days Elapsed', a DAX measure I have made.
I want it to show records if they are category 'A' AND 'Days Elapsed' <30, OR if they are category 'B' and 'Days Elapsed' <10
eg. this table: Before filtering would become this table: After filtering
This would be quite simple to do in Python but as I'm new to DAX measures this is confusing me quite a bit- not sure if I need a new measure or a calculated column.
You can create a measure that applies the filter on the table and count the rows.
Measure =
COUNTROWS(
FILTER(
'Table A',
([Category]="A" && [Days Elapsed]<30) ||
([Category]="B" && [Days Elapsed]<10)
)
)
Keep in mind Measure are computed at runtime, you can do something similar with a calculated column and materialize the filter to make it easier to understand.
Calculated Column = IF(([Category]="A" && [Days Elapsed]<30) ||
([Category]="B" && [Days Elapsed]<10),1)
Then you can filter when that column is 1.

Leave first row as empty and update the value from another column leaving the first row as empty/blank in Power Bi

Create a new column (Column 2) from column 1 with first row as empty or blank
enter image description here
You may need to add 2 more columns:
Ranking:
Rank =
RANKX ( 'Table1', [Column1],, ASC )
and Column 2:
Previous =
LOOKUPVALUE ( Table1[Column1], Table1[Rank], 'Table1'[Rank] - 1 )
You will get the table:
Try this solution:
Create a dummy table with a row that has 'null' as value.
Now union both the tables (Source and Dummy)
Table 3 = UNION('Table 2','Table') // Table -> Source and Table 2 -> Dummy
Create a Rank formula to add row number and sort the required column
IF('Table 3'[ID] = BLANK(), COUNT('Table 3'[ID]), RANKX('Table 3','Table 3'[ID],'Table 3'[ID],ASC)-1)
Here ID is source column of union table, This will add the row number and sort to bring the null record to bottom.
Now use below formula to add the source column as required. using formula from Andrew
LOOKUPVALUE ( 'Table 3'[ID], 'Table 3'[Rank], 'Table 3'[Rank] - 1 )
Add these 3 columns to the visual and change the font color for values and Column to white and reduce the column size to Minimum and rename column name to '.'