DAX create empty table with specific column names and no rows - powerbi

How to create a table with a specified column name and no rows at all. The following oneliner does what I want but shows error message that there should be second argument in ROW function.
EmptyTable = ROW ("Product")
I would like to use it for making bridge tables with desired column name. For example I want Product_bridge table to have a column "Product".
Product_bridge = DISTINCT(
UNION(
DISTINCT( Sales[Prod_Name] )
,DISTINCT( Dictionary[Prod_DifferntName])
,DISTINCT( PriceList[P] )
))
The code above gets me the name of the first table, in this case Prod_Name.

You could just filter it. Or select TOPN 0.
TOPN:
Table = TOPN(0;DATATABLE("Product";STRING;{{}}))
FILTER:
Table = FILTER(DATATABLE("Product";STRING;{{}});0)

This is how I create empty DAX tables:
EmptyTable = DATATABLE (
"Other Measures", INTEGER,
{
{ 0 }
}
)

I would like to add to mxix answer a few useful oneliners for making one-column empty table with desired name:
OneLiner1 = TOPN(0, ROW("Product", "Apple"))
OneLiner2 = FILTER(ROW("Product", "Apple"), 1=2)
Or if you want to define column type:
OneLiner3 = TOPN(0, DATATABLE("Product", STRING,{{"Apple"}}) )
So the snipped for bridge table is:
Product_bridge = DISTINCT(
UNION(
TOPN(0, ROW("Product", "Apple"))
,DISTINCT( Sales[Prod_Name] )
,DISTINCT( Dictionary[Prod_DifferntName])
,DISTINCT( PriceList[P] )
))

Related

DAX Measure: case based on other column text values

I have a big model in PowerBI where there are many different aggregation and grouping based on columns being displayed or not on the final table.
Simplifying: I need to do a conditional statement doing the sum if the value of column 1 is A1 but doing the MAX() if the value of column 1 is A2.
I need to have that information in the same column of the final output.
How would you go for this one?
Thank you very much for your help!
if you have only two values you can do a simple IF like this :
Measure = IF ( SELECTEDVALUE('Table'[Column1]) = "A1", SUM('Table'[Column2]), MAX('Table'[Column2]))
Please try this code:
TblMeasure =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( FactTable[Column1] ),
"Test",
IF (
FactTable[Column1] = "A1",
SUM ( FactTable[Column2] ),
MAX ( FactTable[Column2] )
)
)
RETURN
SUMX ( TblSummary, [Test] )
If we test it on a table visual:

Convert SQL subquery to Power BI DAX

As a newbie of PowerBI, I was so struggled with this problem. I want to create a custom measure, and it can be solved easily with SQL statement which looks like this:
select count(*) from Task
where case_num in(
select case_num from Task
where task_type="DDD") sub
Can somebody help me with this. Thank you!!!!!
The table is roughly like this
You can isolate the case_nums that have a specific task type
VAR tbl = CALCULATETABLE(VALUES('Table'[case_num]),'Table'[task_type] = "DDD")
And then apply that table as a filter:
Measure =
VAR tbl = CALCULATETABLE(VALUES('Table'[case_num]),'Table'[task_type] = "DDD")
RETURN CALCULATE(COUNT('Table'[case_num]), TREATAS(tbl,'Table'[case_num]))
or
Measure =
VAR tbl = CALCULATETABLE(VALUES('Table'[case_num]),'Table'[task_type] = "DDD")
RETURN CALCULATE(COUNT('Table'[case_num]), 'Table'[case_num] IN tbl)
Try this measure
_count =
CALCULATE ( COUNT ( Task[CaseNum] ), FILTER ( Task, Task[task_type] = "DDD" ) )
Edit
If you have a small number of tasks which you want to use in filter, you can use this
Measure =
CALCULATE ( COUNT ( Task[Case] ), FILTER ( Task, Task[task_type] IN {"BB","AA","DDD","CC"}))
If you have a large number of tasks that you don't want to manually type in filter, then create a lookup table first and use that in a measure
TaskLookUpTable = DATATABLE ( --a long list of task; hence taskTbl
"task_type", STRING,
{
{ "BB"},
{ "AA"},
{ "DDD"},
{ "CC"}
}
)
Measure2 = --create the above table first before writing this measure
CALCULATE (
COUNT ( Task[Case] ),
TREATAS ( VALUES ( 'TaskLookUp'[task_type] ), Task[task_type] )
)

TopN, Grouping, Show Others at the bottom POWERBI-DAX

I have the following formula which creates the table in the screenshot below on the left (names of actual tables are different - also it combines 2 separate tables in one) -
Top 11 Jun =
IF (
[Type Rank Jun] <= 11,
[Total Jun],
IF (
HASONEVALUE ( Partners[partner_group] ),
IF (
VALUES ( Partners[partner_group] ) = "Others",
SUMX (
FILTER ( ALL ( Partners[partner_group] ), [Type Rank Jun] > 11 ),
[Total Jun]
)
)
)
)
Now i'm stuck on how to combine the "Null" and "Others" under "Others" and put "Others" at the bottom.i can combine the "Null" & "Others" at each table level, i'm just not sure how.
The DAX solution:
To get the Other and blank (at least that is how I read your null) together, you can create a new column on the table (is easiest).
newProducts = IF(fruits[product] = BLANK(); "Other";fruits[product])
A better solution is to replace your blanks (or NULL) in the Query language:
Go to: Edit Query:
Select your table and the product column and press on the bar the "Replace values"
Do the replace and save and close the editor.
Last step
It is not relevant in which order you have the rows in the table because you can control this in the visual self.
Below example:
As you can see, I filtered other out, this is not needed when you want to count them in your top N.
If you want to show all four, we need to make a new Table:
Tabel =
var Top3 = TOPN(3;FILTER(fruits;fruits[product] <> "Other") ;fruits[July Avail])
var prioTop3 = ADDCOLUMNS(Top3;"Order"; CALCULATE(COUNTROWS(fruits);FILTER(Top3; fruits[July Avail] <= EARLIER(fruits[July Avail]))))
var Other = ADDCOLUMNS(GROUPBY(FILTER(fruits;fruits[product] = "Other");fruits[product];"June Avail"; SUMX(CURRENTGROUP();fruits[June Avail]); "July Avail"; SUMX(CURRENTGROUP();fruits[July Avail]));"Order";0)
return UNION(prioTop3; Other)
Result:

Finding difference between two Tables generated from one table using DAX

I want to find the difference between the sum of all the common elements between 2 tables.
We have one large table (Table A)
You can observe that neither the id, nor the date can be used individually as a unique identifier.
Based on certain filters(one of it is date), we have to create 2 tables:
We then have to find the difference between the sum of all the common elements.
Table 1 and 2 may not be real tables and could be simply virtual tables (defined using VAR) declared in order to create the measure.
I have tried the following code:
Difference =
VAR
Table1= ADDCOLUMNS('TableA', "id", CALCULATE(VALUES('TableA'[Id]), ALL('Date'),ALL('TableA'), USERELATIONSHIP('Date'[As of Date], Previous_Date[Previous_Date]),USERELATIONSHIP('Date'[As of Date], 'TableA'[Date])), "Value", 'TableA'[Value] )
VAR
Table2= ADDCOLUMNS('TableA', "id", CALCULATE(VALUES('TableA'[Id]),ALL('TableA'), USERELATIONSHIP('Date'[As of Date], 'TableA'[Date])),"Value", 'TableA'[Value] )
VAR
abc = CALCULATE(SUMX(Table1,IF(VALUES('TableA'[Id]) IN Table1 && VALUES('TableA'[Id]) in Table2,Table1[Value])) )
VAR
pqr = CALCULATE(SUMX(Table2,IF(VALUES('TableA'[Id]) IN Table1 && VALUES('TableA'[Id]) in Table2,Table2[Value])) )
RETURN
abc-pqr
Pre-requisite :- Change the Data Type of your id columns to Text in both the tables.
Then, Create this Calculated Column in Table1 :-
Common_NotCommon_Table1 =
Var out1 = LOOKUPVALUE(Table2[id],Table2[id],Table1[id])
Var out2 = IF(out1 <> "", "Common","Not-Common")
return out2
Then Create this Calculated Column in Table2 Likewise :-
Common_NotCommon_Table2 =
Var out1 = LOOKUPVALUE(Table1[id],Table1[id],Table2[id])
Var out2 = IF(out1 <> "", "Common","Not-Common")
return out2
Then Create the two Measures to find the Sum - this can be created in any of your tables.
Sum_Common_Table1 = CALCULATE(SUM(Table1[corresponding value]), FILTER(Table1, Table1[Common_NotCommon_Table1] = "Common"))
Sum_Common_Table2 = CALCULATE(SUM(Table2[corresponding value]), FILTER(Table2, Table2[Common_NotCommon_Table2] = "Common"))
Then Create the Difference Measure in any one of the Table:-
Common_Diff = [Sum_Common_Table2] - [Sum_Common_Table1]
The Output as Expected looks like,
Kindly accept the answer if it helps and let me know, how it works for you.

DAX to lookup date in ValidFrom column

I have two (2) tables
Tablename: X
ID Name ValidFrom Property
A-----Test1-----01.01.2010---------30
A-----Test1-----01.01.2015---------60
B-----Test1-----01.01.1900---------30
B-----Test2-----01.01.2018---------60
Tablename: Y
ID Date
A---01.01.2010
A---01.02.2010
A---01.03.2015
A---01.04.2015
Ideally, I would like to add calculated columns to Table Y which looks up ID and date with ID and ValidFrom from Table X. In this example, row#1 in Table X would be the returning row of data for all dates >= 01.01.2010 and dates < 01.01.2015. The resulting outcome would be like this:
Tablename: Y (new)
ID Date Name Property
A---01.01.2010----Test1------30
A---01.02.2010----Test1------30
A---01.03.2015----Test1------60
A---01.04.2015----Test1------60
Any help would be greatly appreciated
It's not clear how the Name column is generated, but here's how you can get the Property column once you have the Name column in table Y:
Property =
VAR LastValid =
CALCULATE (
MAX ( X[ValidFrom] ),
FILTER (
ALL ( X[ValidFrom] ),
X[ValidFrom] <= EARLIER ( Y[Date] )
)
)
RETURN
LOOKUPVALUE (
X[Property],
X[ID], Y[ID],
X[Name], Y[Name],
X[ValidFrom], LastValid
)
The LastValid variable finds the latest date that is less than or equal to the date in the current row. Then you use that along with the ID and Name to look up the Property from table X.
Taking in the last formula and applying the LOOKUPVALUE to the ID and ValidFrom as you refers, I tried successfully the next approach:
Name =
VAR LastValid =
CALCULATE(MAX(X[ValidFrom]);
FILTER (
ALL(X[ValidFrom] );
X[ValidFrom] <= EARLIER (Y[Date] )
)
)
RETURN
LOOKUPVALUE (
X[Name];
X[ID]; Y[ID];
X[ValidFrom]; LastValid
)