Power BI NATURALINNERJOIN override active relationship with USERELATIONSHIP - powerbi

I have the following tables with 3 relationships (1 active, 2 inactive):
Table 1 (Relationship: Column 1)
Table 2 (Relationship: Column 1, Column 2, Column 3)
The active relationship is on Column 1 of Table 1 and Table 2. I join these tables like this:
NATURALINNERJOIN(SELECTCOLUMNS('Table 1', "Column 1", 'Table 1'.[Column 1] & ""), SELECTCOLUMNS('Table 2', "Column 1", 'Table 2'.[Column 1] & ""))
Works great!
I want to do another NATURALINNERJOIN but this time on 'Table 1'.[Column 1] and 'Table 2'.[Column 2]. How do I use USERELATIONSHIP to override the active relationship and use the inactive ones?

You don't need to override the relationship if you're creating a calculated table. Just specify your desired columns. e.g.
Table3 =
NATURALINNERJOIN(SELECTCOLUMNS('Table1', "Column 1", 'Table1'[Column1] & ""), SELECTCOLUMNS('Table2', "Column 1", 'Table2'[Column3] & ""))

Related

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

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.

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.

PowerBI - how to check if an entry with few conditions is present in anther table or not

I have two tables
Table A
ID user price
1 A 10
2 A 10
1 C 15
3 A 15
Table B
ID User price
2 A 20
1 A 10
1 B 15
3 A 15
I need to add a new column to see if a row is present in the other table or not.
Exisits? = IF(CALCULATE(COUNTROWS('Table B'), FILTER('TABLE B', 'TABLE B'[ID] = 'TABLE A'[ID]), FILTER('TABLE B', 'TABLE B'[user] = 'TABLE A'[user]), FILTER('TABLE B', 'TABLE B'[price] = 'TABLE A'[price])) > 0 , "Yes", "No")
But I don't know if there is an issue between the relationships and I don't know why a relationship even matters for such a DAX formula because I have so many linking between the tables where it is not exactly a relationship but I create a relationship just for the sake of getting correct results for DAX. But all the results for the above DAX is "No". Why? Obviously, there is are entries in Table B that are in Table A.

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 '.'

dcast / melt / reshape a table which has calculated columns in power BI using DAX

I have a table with 5 columns
Store ID | Year | Sales Group 1 | Sales Group 2 | Sales Group 3
All fields with Sales Group are calculated using DAX. I would like to create a new table which contains Store ID, Year, Sales Group and Sales Value.
So essentially I would have 3 rows of data for each store ID and Year, each containing sales value for a different sales group
I want a DAX query to convert from Table 1 to Table 2
Table 1:
Table 2:
It would probably be better to do this in Power Query M, which has a built in unpivot transform, but you can get there with DAX:
UnpivotedTable =
// GROUPBY gives you unique combinations of the columns referenced
VAR StoreYears =
GROUPBY (
'Table',
'Table'[Store ID],
'Table'[Year]
)
RETURN
// UNION does what it says on the label, unions multiple tables
UNION (
// ADDCOLUMNS is also self-descriptive - takes a table, adds columns to it
ADDCOLUMNS (
StoreYears,
// After the table arg, ADDCOLUMNS takes pairs of quoted column name and
// DAX expression to evaluate for the value in that column. We create
// two columns, the group and the sum of the source column for that group.
"Sales Group", 1,
"Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 1] ) )
),
// repeat the pattern above per group
ADDCOLUMNS (
StoreYears,
"Sales Group", 2,
"Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 2] ) )
),
ADDCOLUMNS (
StoreYears,
"Sales Group", 3,
"Sales Value", CALCULATE ( SUM ( 'Table'[Sales Group 3] ) )
)
)
Best way to do this is in the Query Editor (Performance). Load your data, go to query editor and select the table.
Select the 3 columns of the sales group, go to Transform -> unpivot
Rename the column headers
End result: