I have two tables:
table 1:
Product LOB
BVPN NS
SD-WAN IS
QUICK START NS
BVPN SMALL OSBU
Table 2:
Product LOB
BVPN NS
SD-WAN IS
QUICK START NS
BVPN SMALL NS
I want to create a custom column that will change the value "OSBU" in LOB column of table1 to NS based on the value in LOB column of table2 and keep other values the same. I used the following code but it's not giving me the desired output. Can anyone tell what is wrong?
Column =
IF (
'table1'[LOB] = "OSBU",
RELATED ( 'table2'[LOB] ),
'table1'[GOLD_BILLING_PROFILE.Product/Service]
)
RELATED function works between tables with a relationship established only. You would have to create a relationship between Table1 and Table2 based on Product and hopefully it is a one to one mapping. The following link should give you the basic details on creating and managing a relationship:
https://learn.microsoft.com/en-us/power-bi/desktop-create-and-manage-relationships
Hope this helps.
Edit:
I don't know why you are using a different variable for the FALSE Condition. Ideally it should be something like:
Column =
IF (
'table1'[LOB] = "OSBU",
RELATED ( 'table2'[LOB] ),
'table1'[LOB]
)
Related
I am using DAX Studio and I would like to add a filter to the table field in SELECTCOLUMNS so that it using two different table depending on the filter's expression result.
In other words what I would like to do is similar to the following :
DEFINE
VAR cond_talble =
SELECTCOLUMNS(
IF(#param1="1",TABLE1,TABLE2),
"column1",[column1],
"column2",[column2]
)
Thank you kindly
there is a work around for this problem but might not be a good one for everyone, and it's to add a column in both tables containing a boolean that is set to true for table1 and 0 for table2 and then (if your trables contains the same columns like me) get a table that is the fruit of the union of both tables and add an if condition on a filter so that you filter with the added column
I have three different tables. I want to select different columns from each of the tables and create one table based on some filters. Have a look at the following dax expression:
FILTER(DISTINCT(SELECTCOLUMNS(Test_Table,"site_key",[site_key],"is_active",[is_active])),[is_active]=TRUE&&[dbsource]=="DB2")
As you can see, I've selected olumns from Test_Table.
Firstly, How can I add columns from the other two tables?
Secondly, please note that these tables are related so I've created a relationship between them based on relevant IDs.
Will I have to create a natural join in DAX as well?
As I mentioned in the comment, SUMMARIZECOLUMNS can probably get you what you're looking for. It might look something like this in your case:
SUMMARIZECOLUMNS (
Test_Table[site_key],
Test_Table_2[industry_group],
Test_Table_2[country],
Test_Table_2[state],
FILTER ( Test_Table, [is_active] = TRUE && [dbsource] = "DB2" )
)
I have a table Deals which has columns [DealId], [Open Date Id], [Closed Date Id] where the last 2 columns are like a foreign key to the Date table which has [Date], [DateId] column.
Power BI won't let me have 2 active relationship, so one is inactive.
Now I want to create some visuals indicating the deals that were open and closed in a custom range of time (using slicer).
How I tried to solve
The closest solution to this was creating a calculated column with the LOOKUPVALUE and adding the close and open dates directly to Deals table. And created 2 different pages with 2 different slicers, but this is far not the solution I wanted.
How can I solve this problem?
I don't know if what I'm going to say suits your needs based on the size of the tables or the rigidity of the data model due to other measures. I think that in the end what matters is to understand what are the limitations of what you want to show. However, something almost similar I answered here: https://stackoverflow.com/a/66792957/15460989
From what I could understand you have two tables similar to:
Deals = {[DealID] [OpenDate] [CloseDate] [Quantity] [Price] ...}
Dates = {[Date] [MonthName] [MonthNumber] [Year] ...}
And you want to filter Deals based on two relationships
USERELATIONSHIP (Dates [Date], Deals [OpenDate])
USERELATIONSHIP (Dates [Date], Deals [CloseDate])
I am not going to discuss the option of duplicating Dates Table because it was previously covered using two slicers.
But what if the characteristics of my model allow me to use a table with two relationship (One active and the other inactive) while my visualization uses the content of an unrelated table?
Let's define my new unrelated table as:
HarvestingDates = {[Date] [MonthName] [MonthNumber] [Year] ...}
and what I'm trying to achieve is something like this:
From a model like this one:
Deals[DealID]: Unique values.
Deals[OpenDate]: Repeated and missing dates
Deals[CloseDate]: A random number between 0 and 5 is added to Deals [OpenDate]
Instead of choosing an opening date and a closing date, I choose a date range not related to the model and the context related to the deals comes from the measures. Example:
Opened Deals: All the deals opened in a certain date range and summarized by the visualization.
HOpenedDeals: =
CALCULATE(
COUNTROWS(Deals),
TREATAS(
VALUES(HarvestingDate[Date]),Dates[Date]
)
)
Closed Deals: All the deals closed in a certain date range and summarized by the visualization.
HClosedDeals:=
CALCULATE(
COUNTROWS(Deals),
USERELATIONSHIP(Dates[Date],Deals[CloseDate]),
TREATAS(VALUES(HarvestingDate[Date]),Dates[Date])
)
Open and closed deals: All open and closed deals in the same date range summarized by the visualization
HOpened&Closed :=
VAR TotalRow= SUMMARIZE(HarvestingDate,HarvestingDate[Year],HarvestingDate[MonthName])
VAR CurrentDates=VALUES(HarvestingDate[Date])
VAR Result=
ADDCOLUMNS(TotalRow, "Count",
VAR CurrentMonthName= {CALCULATE(VALUES(HarvestingDate[MonthName]))}
VAR CurrentYear= {CALCULATE(VALUES(HarvestingDate[Year]))}
RETURN
COUNTROWS(INTERSECT(
CALCULATETABLE(Deals,
USERELATIONSHIP(Dates[Date],Deals[CloseDate]),
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
),
CALCULATETABLE(Deals,
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
)
)))
RETURN SUMX(Result,[Count])
Opened & Not Closed Deals: All open and non-closed deals in the same date range summarized by visualization
HO&NOTC :=
VAR TotalRow= SUMMARIZE(HarvestingDate,HarvestingDate[Year],HarvestingDate[MonthName])
VAR CurrentDates=VALUES(HarvestingDate[Date])
VAR Result=
ADDCOLUMNS(TotalRow, "Count",
VAR CurrentMonthName= {CALCULATE(VALUES(HarvestingDate[MonthName]))}
VAR CurrentYear= {CALCULATE(VALUES(HarvestingDate[Year]))}
RETURN
COUNTROWS(EXCEPT(
CALCULATETABLE(Deals,
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
),
CALCULATETABLE(Deals,
USERELATIONSHIP(Dates[Date],Deals[CloseDate]),
TREATAS(CurrentMonthName, Dates[MonthName]),
TREATAS(CurrentYear, Dates[Year]),
TREATAS(CurrentDates, Dates[Date])
)
)))
RETURN SUMX(Result,[Count])
TEST
Date range: {5/27/2021…5/31/2021}
I am sure this can be improved but as I said at the beginning it is just an idea. Cheers!
In this case the easiest way is to implement a role playing dimension functionality by duplicating your date table. Power BI engine does not support role playing dimensions, so the workaround for small tables is just duplicating them, as described in this article.
In your case, you could create a table "Date_for_closed" using
Date_for_closed = ALL('Date')
This creates a copy of your original Date table. Then you can create relationships and only have active ones. This way it is even easier to maintain than a bunch of inactive relationships.
With this implemented you can build this:
from this source:
I have two tables (CompletedJobs & ScriptDetails) and using DAX, I want to return distinct Names that appear in CompletedJobs that do not appear in ScriptDetails.
Here is my SQL Query. Works and return values.
Select Distinct CJ.[Name]
From CompletedJobs CJ
Left Join ScriptDetails SD
ON CJ.[Name]=SD.ActivityName
Where SD.ActivityName IS NULL
I started with creating the following DAX query, but just doing this, I get the following error message:
"A table of multiple values was supplied where a single value was expected"
AdhocJobs = DISTINCT(UNION(SELECTCOLUMNS(CompletedJobs,"Name",CompletedJobs[Name]),SELECTCOLUMNS(ScriptDetails,"Name",ScriptDetails[ActivityName])))
How do I create a DAX query that would replicate the SQL query?
Rather than recreate your SQL, there is DAX that already addresses your specific use case. The EXCEPT function returns a table where rows from the LEFT SIDE table do not appear in the RIGHT SIDE table.
EVALUATE
DISTINCT (
EXCEPT (
SUMMARIZE ( CompletedJobs , CompletedJobs [Name]),
SUMMARIZE ( ScriptDetails , ScriptDetails [ActivityName])
)
)
In this case I used SUMMARIZE to reduce each table down to one column, and then wrapped them with EXCEPT to take only the Names from Completed Jobs that aren't ActivityNames in ScriptDetails.
Hope it helps.
I have 2 queries:
Premium:
and Losses:
How can I simply summarize data from Premium query and LEFT JOIN it to summarized data in Losses query using DAX?
In SQL it would be like that:
declare #PremiumTable table (PolicyNumber varchar(50), Premium money)
insert into #PremiumTable values
('Pol1', 100),
('Pol1', 50),
('Pol2', 300),
('Pol3', 500),
('Pol3', 200),
('Pol4',400)
declare #LossesTable table (PolicyNumber varchar(50), Losses money)
insert into #LossesTable values ('Pol1',115),
('Pol1',25),
('Pol2',0),
('Pol3',110),
('Pol3',75)
select p.PolicyNumber,
sum(p.Premium) as Premium,
sum(l.Losses)as Losses
from #PremiumTable p
LEFT JOIN #LossesTable l on p.PolicyNumber = l.PolicyNumber
group by p.PolicyNumber
Result:
I tried using NATURALLEFTOUTERJOIN but it gives me an error:
*An incompatible join column, (''[PolicyNumber]) was detected. 'NATURALLEFTOUTERJOIN' doesn't support joins by using columns with different data types or lineage.*
MyTable =
VAR Premium =
SELECTCOLUMNS(
fact_Premium,
"PolicyNumber",fact_Premium[PolicyNumber],
"Premium", fact_Premium[Premium]
)
VAR Losses =
SELECTCOLUMNS(
fact_Losses,
"PolicyNumber", fact_Losses[PolicyNumber],
"Losses", fact_Losses[PaymentAmount]
)
VAR Result = NATURALLEFTOUTERJOIN(Premium,Losses)
RETURN Result
There are a few interdependent "bugs" or limitations around the use of variables (VAR) and NATURALLEFTOUTERJOIN which makes this a weird case to debug.
Some notable limitations are:
VAR:
Columns in table variables cannot be referenced via
TableName[ColumnName] syntax.
NATURALLEFTOUTERJOIN:
Either:
The relationship between both tables has to be defined before the
join is applied AND the names of the columns that define the
relationship need to be different.
Or:
In order to join two columns with the same name and no relationships,
it is necessary that these columns to have a data lineage.
(I'm a bit confused because the link mentioned do not have a data lineage; while official documentation said only columns from the same source table (have the same lineage) are joined on.)
Come back to this case.
SUMMARIZE should be used instead of SELECTCOLUMNS to obtain summary tables for Premium and Losses, i.e.:
Premium =
SUMMARIZE(
fact_Premium,
fact_Premium[PolicyNumber],
"Premium", SUM(fact_Premium[Premium])
)
Losses =
SUMMARIZE(
fact_Losses,
fact_Losses[PolicyNumber],
"Losses", SUM(fact_Losses[Losses])
)
When we apply NATURALLEFTOUTERJOIN to the above two tables, it'll return error No common join columns detected because of they have no relationship established.
To resolve this, we can make use of TREATAS as suggested in this blog post. But to use TREATAS, we have to reference the column names in Premium and Losses table, so we can't use VAR to declare them, but have to actually instantiate them.
To conclude, the solution would be:
Create calculate tables for Premium and Losses as mentioned above.
Use TREATAS to mimic a data lineage and join Premium table with Losses_TreatAs instead.
MyTable =
VAR Losses_TreatAs = TREATAS(Losses, Premium[PolicyNumber], Losses[Losses])
RETURN NATURALLEFTOUTERJOIN(Premium, Losses_TreatAs)
Results:
There's a sleazy hack that can successfully work around this awful limitation (what were the product designers thinking?).
If you add zeros (e.g. + 0) or concatenate an empty string (e.g. & "") to each join column within SELECTCOLUMNS, it breaks out of the data lineage straitjacket and runs the NATURALLEFTOUTERJOIN just using column names.
You can use this in a Measure to run dynamic logic (based on the query context from filters etc), not just while creating a calculated table.
Here's an tweaked version of your code:
MyTable =
VAR Premium =
SELECTCOLUMNS(
fact_Premium,
"PolicyNumber",fact_Premium[PolicyNumber] & "",
"Premium", fact_Premium[Premium]
)
VAR Losses =
SELECTCOLUMNS(
fact_Losses,
"PolicyNumber", fact_Losses[PolicyNumber] & "",
"Losses", fact_Losses[PaymentAmount]
)
VAR Result = NATURALLEFTOUTERJOIN(Premium,Losses)
RETURN Result
H/T to example #7 on this page, which shows this in code (without really explaining it).
https://www.sqlbi.com/articles/from-sql-to-dax-joining-tables/#code7
Hello I suggest you this way:
in PowerQuery, built up a table with policyNumber like that:
Duplicate Premium table, and remove the premium column on the duplicate. Call it PremiumPol
Duplicate the Losses table, and remove the losses column on duplicate. Call it LossesPol
Then use the button Append Query, to Append PremiumPol and LossesPol. Call it policynumber
Last remove duplicate from the appended tables
Then click on close and Apply
Check that your model is like that:
Then, to add losses and premium on a policy base is trivial, go on and select a table visual and these fields:
the result is like this:
Hope that helps!