Power BI - SUM IFF Across two (not related) tables - powerbi

I need help about Power bi.
PROBLEM:
I have two tables (most of the fields in the tables are the same, but tables are not equal) , I have Unit name in both tables, and I need a table (output) that contains each Unit and the COUNT of the unit (how many time that unit is in table 1 and in table 2) based on 2 conditions:
I have to count only units that have status not equal to "rejected".
I have to count only units that have the Department_Name = "TX"
I used this DAX measure (but it didn't work):
Measure = calculate(
COUNT(Table 1[Unit_Name]) + COUNT(Table 2[Unit_Name]),
Table 1[Status] <> "rejected",
Table 2[Status] <> "rejected",
Table 1[Department_Name] <> "TX",
Table 2[Department_Name] <> "TX")
The purpose is to use the measure in a PowerBI chart and insert on the X axis the measure, on the Y axis the unit_name (I don't know if I have to use unit_name by Table 1 or unit_name by table 2).
I also tried to create a connection between tables using unit_name as primary key.
IMAGE

there are many ways to do that but the quickest and easiest way is to merge the two tables in to one like
go into the power Query editor , and merge the two tables into one using Power Query Script
for details on how to merge the two tbale data set in power Query see the below link :
https://www.c-sharpcorner.com/article/merge-two-tables-in-power-bi/
video tutorial :
https://www.youtube.com/watch?v=cX62HBIZdPA
after doing this your table will be one instead of 2 tables and you can easily perform the count on single column "Department Name" for the Unit occurance.

Using DAX, It is a 2 step process:
First Create a new table with this DAX Code:
FullJoin = UNION(Table1,Table2)
Your Table will look like this:
Then Create a measure with DAX Code like this:
Solution = CALCULATE(
COUNTX(FullJoin,[Unit_Name]),
FullJoin[Status] <> "rejected",
FullJoin[Department_Name] = "TX"
)
Now If we test it on a visual,

Related

Making DAX calculated table respond to filters on other table

I have a DAX table "Sumtable" that calculates the the number of rows in "Maintable" for two cases: (1) all rows in Maintable and (2) the subset of rows where Cat = "A".
Sumtable = {
("Full set", COUNTROWS(Maintable)),
("Subset", COUNTROWS(FILTER(Maintable, Maintable[Cat] = "A")))
}
I want to viz Sumtable and make it respond to filter settings on Maintable. For example when I select Maintable[Sex] = "Male" the totals in Sumtable should reflect that. What is the right way to accomplish this?
For example when I select Maintable[Sex] = "Male" the totals in Sumtable should reflect that.
Just make this two measures instead of a calculated table.
If you want these measures to appear under Sometable, you can create a one-row, one-hidden-column table with two measures, instead of two calculated columns.
And when you hide all the non-measure columns on a table it becomes a "Measure Table" with a differentiated icon.
Solved it with this: analytics-tuts.com/bar-chart-using-measures-in-power-bi

Power BI - If a date is between 2 dates using relationships

I am trying to determine if a date is between 2 other dates that are is 2 different tables using
Power BI.
For simplicity, here is the model that I have :
TableB is the bridge table between TableA and TableC.
I have an inactive relationship between tableA and TableC.
I have tried the following logic to check if TableC.createdDate is between TableA.startDate and TableA.endDate :
Create a calculated column in TableC, but I was not able to access columns in TableA
Create a calculated column in TableB, but I was having blank results, which is not suppose to happen
Have you tried using USERELATIONSHIP to force your calculated column to use the inactive relationship to TableA? documentation link
So something like
CALCULATE(
{{created date is >start and <end}},
USERELATIONSHIP('TableA'[TableB_ID],'TableC'[TableB_ID]
)

How do I use an IF THEN function in Power BI over multiple queries?

I am trying to filter an average amount (wage) from one query through a column (gender; 0 = male, 1 = female) belonging to different query.
So, I want put the different averages (query 1) for both genders (query 2) into one card or bar chart visualization.
Can I do this using DAX? Or do I need to merge the queries somehow?
Thank you!
Assuming you mean Tables vs Queries. If you have a relationship between the table with the Gender and a table with the averages, then you just need to include the gender column and the average column in your results.

Power BI - Select Slicer Date Between 2 Columns

Hopefully a quick explanation of what I am hoping to accomplish followed by the approach we've been working on for over a year.
Desired Result
I have a table of SCD values with two columns, SCD_Valid_From and SCD_Valid_To. Is there a way to join a date table in my model (or simply use a slicer without a join) in order to be able to choose a specific date that is in between the two SCD columns and have that row of data returned?
Original Table
ID | SCD_Valid_From | SCR_Valid_To | Cost
1 2020-08-01 2020-08-03 5.00
Slicer date chosen is 2020-08-02. I would like this ID=1 record to be returned.
What We've Attempted So Far
We had a consultant come in and help us get Power BI launched last year. His solution was to create an expansion table that would contain a row for every ID/Date combination.
Expanded Original Table
ID | SCD_Valid_Date | Cost
1 2020-08-01 5.00
1 2020-08-02 5.00
1 2020-08-03 5.00
This was happening originally on the Power BI side, and we would use incremental refresh to control how much of this table was getting pushed each day. Long story short, this was extremely inefficient and made the refresh too slow to be effective - for 5 years' worth of data, we would need over 2000 rows per ID just to be able to select a dimensional record.
Is there a way to use a slicer where Power BI can select the records where that selected date falls between dates in two columns of a table?
Let me explain a workaround and I hope this will help you to solve your issue. Let me guess you have below 2 tables-
"Dates" table with column "Date" from where you are generating the date slicer.
"your_main_table" with with column "scd_valid_from" and "scd_valid_to".
Step-1: If you do not have relation between table "Dates" and "your_main_table", this is fine as other wise you have to create a new table like "Dates2". For this work around, you can not have relation between those tables.
In case you have already relation established between those tables, create a new custom table with this below code-
Dates2 =
SELECTCOLUMNS(
Dates,
"Date", Dates[Date]
)
From here, I will consider "Dates2" as source of your Date slicer. But if you have "Date" table with no relation with table "your_main_table", just consider "Dates" in place of "Dates2" in below measures creation. Now, Create these following 4 measures in your table "your_main_table"
1.
date_from_current_row = max(join_using_date_range[SCD_Valid_From])
2.
date_to_current_row = max(join_using_date_range[SCD_Valid_to])
3.
date_selected_in_slicer = SELECTEDVALUE(Dates2[Date])
4.
show_hide_row =
if(
[date_selected_in_slicer] >= [date_from_current_row]
&& [date_selected_in_slicer] <= [date_to_current_row]
,
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "your_main_table"
Final Step: Now just add a visual level filter with the measure "show_hide_row" and set value will show only when "show_hide_row = 1".
The final output will be something like below image-

Calculated Column to get average from values in another table in PowerBI

This might be very basic but I am new to PowerBI.
How do I get average of values for unique ID into another table.
For eg. My Table 1 has multiple ID values. I have created another table for unique ID which I am planning to used to join other table.
I want a calculated column in table 2 which will give me average value of respective ID from table 1.
How do I get the calculated column like shown below
In stead of creating a new table with the averages per ID and then joining on that, you could also do it directly with a calculated column using the following DAX expression:
Average by ID = CALCULATE(AVERAGE('Table 1'[Values]),ALLEXCEPT('Table 1','Table 1'[ID]))
Not exactly what you asked for, but maybe it's useful anyway.
How's it going?
The quickest way I can think of doing this would be to use
SUMMARIZECOLUMNS
You can accomplish this by creating another table based on your initial fact table like so:
Table 2 =
SUMMARIZECOLUMNS ( 'Table 1'[ID], "Avg", AVERAGE ( 'Table 1'[Values] ) )
Once this table has been created, you can create a relationship.
This will work in either SSAS or in PowerBI directly.
Hope this helps!! Have a good one!!