power bi lookup and match value from another table - powerbi

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.

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

How is measure able to filter dimension via fact table?

I have 2 tables: dimProduct and factSales
Dim table has productid, name, category
Fact table has salesid, productid, status, amount, paiddate
I have a table visual that shows the status from the fact table. Against each status I want to show the count of products and the count of distinct category.
CountProducts=DISTINCTCOUNT(FACTSALES[PRODUCTID])
CountDistinctCategory=DISTINCTCOUNT(dimProduct[category])
How to correct this?
Set the cross-filter direction for your relationship to 'Both', so that filtering can also propagate from the fact table to the dimension table.
Alternatively, using DAX:
CountDistinctCategory =
CALCULATE(
DISTINCTCOUNT( dimProduct[category] ),
TREATAS(
VALUES( factSales[productid] ),
dimProduct[productid]
)
)

How do i bring userelationship in this calculation

I am trying to calculate the number of working days between 2 date columns in a table called Incident Table and the 2 columns are called Created Date and Resolved Date. I also have a standard date table.
Right now, the relationship is one to many between date and Created date column in Incident table
First I created a column in date table that would give true or false as to whether a day was a weekday
Is Working Day = if('Date'[Day Name Short]="Sat",FALSE(),if('Date'[Day Name Short]="Sun",FALSE(),TRUE()))
Then in the incident table i used this
Time to Resolve (days) = COUNTROWS ( FILTER ( 'Date', AND ( AND ( 'Date'[Date] >= 'Incidents'[Created Date], 'Date'[Date] <= Incidents[Resolved Date] ), 'Date'[Is Working Day] ) ) )
This formula is fine on its own but i need to connect resolved date with the date table so i can use date filter for both created and resolved. Any ideas?
You would ideally create two relationship from the Dates table with Incident table, one active and one inactive and then in the measure you would activate the inactive relationship using USERELATIONSHIP.
Something =
CALCULATE (
DISTINCTCOUNT ( Incident[Time to Resolve (days)] ),
USERELATIONSHIP ( Incident[Resolved Date], Dates[Date] )
)

Top 2 by group in DAX Power BI

I am trying to get the Top 2 units by company here. Table is called 'Table (3)'
I want to be able to populate the column like this -
I had tried Column = RANKX(ALLEXCEPT('Table (3)','Table (3)'[Company]),SUM('Table (3)'[Units])) but got a circular error.
The other way I think of doing this - not very effective - is use the TOPN and do a UNION by each company so each company would have a table of it's own TOPN value.
I know how to do this on power query already using Table.MaxN but want to do this on DAX
You can get the rank like this:
Rank =
VAR CurrUnits = 'Table (3)'[Units]
RETURN
CALCULATE (
RANK.EQ ( CurrUnits, 'Table (3)'[Units] ),
ALLEXCEPT ( 'Table (3)', 'Table (3)'[Company] )
)
From there, you can throw away ranks other than 1 and 2 if you choose to.