PowerBI: Create a new table using two columns from two different tables - powerbi

I'm struggling a little but with this one. What I'm trying to is to create a new table using two columns of ID's from two different tables which I can then compare for duplicate values, highlight them and show those that are not dups.
I used the following DAX to create a new table
UNION (
SELECTCOLUMNS ( FAACD, "Column1", [best_assignment] ),
SELECTCOLUMNS ( FAACD_All, "Column2", [doc_id] )
)
But the union command is just stacking up everything all under the one column. How can i get the two columns to show separately?
I tried to use SUMMARIZE
New Table =
UNION (
SUMMARIZE ( Table1, [Group_by_Me], "Column1", [Column1] ),
SUMMARIZE ( Table2, [Group_by_Me], "Column2", [Column2] )
)
this put things in separate columns but i didn't give me the result i wanted, i just want two unfiltered columns in a new table

How about using
UNION (
SELECTCOLUMNS ( FAACD, "Column1", [best_assignment], "Column2", BLANK() ),
SELECTCOLUMNS ( FAACD_All, "Column1", BLANK(), "Column2", [doc_id] )
)

Related

Left Joining Tables in Power BI based on multiple columns with duplicate values (DAX)

I am trying to join 2 tables (left join) in Power BI using DAX, and I keep on encountering errors.
The dummy table structure and desired outcome are below:
How would I join the two tables where the Application and Business Unit should match in Both Tables and considering that Table A & B contain duplicate values?
One of the statements I have tried is below, however I keep on encountering errors regarding duplicate values and I am not sure how to proceed. I can't use Power Query unfortunately, this has to be in DAX.
TABLE =
GENERATEALL (
Table_A,
CALCULATETABLE (
ROW (
"New vs Old", VALUES (Table_B[New vs Old]),
"Project", VALUES (Table_B[Project])
),
TREATAS ( ROW ( "Business Unit", Table_A[Business Unit] ), Table_B[Business Unit] ),
TREATAS ( ROW ( "Application", Table_A[Application] ), Table_B[Application] )
)
)
Thank you
Use NATURALLEFTOUTERJOIN Like this:
Table =
var TABLE_A = SELECTCOLUMNS(
{
("A","BU1","2022-10",100),
("B","BU2","2022-11",200),
("B","BU3","2022-10",100),
("C","BU1","2022-11",400),
("D","BU2","2022-12",50)
},"Application",[Value1],"Business Unit",[Value2], "Month",[Value3], "Cost",[Value4])
var TABLE_B = SELECTCOLUMNS(
{
("A","BU1","Project 1","New"),
("B","BU2","Project 2","New"),
("B","BU2","Project 5","Old"),
("B","BU3","Project 3","Old"),
("C","BU1","Project 1","Old"),
("D","BU2","Project 4","New")
},"Application",[Value1],"Business Unit",[Value2], "Project",[Value3], "New vs Old",[Value4])
return NATURALLEFTOUTERJOIN(TABLE_A,TABLE_B)
If your column names or data types don't match you'll need to CONVERT and rename them before joining.
You need to create a Calculated Column that introduces a Union between "Applications" and "Business Unit", in both tables, then introduce tge relationship with this column created. It’s like creating a Primary Key.

DAX syntax - Countrows using allexcept filter

I am using three different measures in the same visual (clustered column chart), but in one of the measures I want to leave out the filter which is used in the other measures. That's why I can't use filter on visual because in one of the measures I don't want this to be used.
I am counting rows, a specific number in 'x_channel' column, but I only want to count rows that 'does not contain "3-"' from column "associate.name" in the same table (TICKET).
How do I add this filter in the following syntax:
E-post = CALCULATE(COUNTROWS(TICKET), TICKET[x_channel]=2, USERELATIONSHIP(DIM_DATO[Opprettet], TICKET[Created]))
I think the syntax should be something like this:
E-post = CALCULATE(COUNTROWS(TICKET), TICKET[x_channel]=2 && ALLEXCEPT(TICKET, TICKET[ASSOCIATE.name]="3-"), USERELATIONSHIP(DIM_DATO[Opprettet], TICKET[Created]))
Thank you!
Usage of ALLEXCEPT is totally different. According to the docs and dax.guide
ALLEXCEPT - Returns all the rows in a table except for those rows that are affected by the specified column filters.
So with this function, you can manipulate filter context to remove all filters from the given table but still keep filters from the column provided to ALLEXCEPT function.
The syntax would be like
Measure =
CALCULATE(
COUNTROWS( TABLE1 ),
ALLEXCEPT( TABLE2, TABLE2[ColumnName] )
)
For your case, try this one:
E-post =
CALCULATE(
COUNTROWS( TICKET ),
TICKET[x_channel] = 2,
TICKET[ASSOCIATE.name] <> "3-",
USERELATIONSHIP( DIM_DATO[Opprettet], TICKET[Created] )
)
or you can use FILTER with ALL and then COUNTROWS as follows
E-post =
CALCULATE(
COUNTROWS(,
FILTER(
ALL( TICKET ),
TICKET[x_channel] = 2 && TICKET[ASSOCIATE.name] <> "3-"
)
),
USERELATIONSHIP( DIM_DATO[Opprettet], TICKET[Created] )
)
In fact, the FILTER with ALL is used under the hood in the first scenario.

Conditional Filtering on Report Data in Power BI

I have a table entry for multiple Product and Many different features. All the data has date time in shown format.
As we can see that all products has entries across different time. My aim is to filter the details related to the product by latest time of everyday.
I was able to split the Time col in Date col & Time col separately.
You can achieve that by creating a new table in Dax for eg. (because I don't know what do you want to do with rows with the same timestamp I show both rows for C)
YourFilteredTable =
VAR __trt =
TREATAS (
SELECTCOLUMNS (
ADDCOLUMNS (
SUMMARIZE (
ADDCOLUMNS (
ALL ( YourTable[Product], YourTable[Time] ),
"day", DATEVALUE ( YourTable[Time] )
),
YourTable[Product],
[day]
),
"MaxDate", CALCULATE ( MAX ( YourTable[Time] ) )
),
"Prod", [Product],
"MaxDate", [MaxDate]
),
YourTable[Product],
YourTable[Time]
)
RETURN
SUMMARIZECOLUMNS (
YourTable[Product],
YourTable[Quant],
YourTable[Time],
__trt
)

Create table with summarization and latest value of the year

For each ClientNo I want the Type classification corresponding to the last date of each Year:
Thus, the table above should be summarized as:
So, somehow, we need two intermediate tables:
Unique values for years, like VALUES(Table[Date].Year)
Unique values for ClientNo, like VALUES(Table[ClientNo])
Then for each combination of Year and ClientNo, get the latest date for each year and finally get the Type classification.
You should be able to do this in two steps along these lines:
Summary =
VAR MaxDates =
SUMMARIZE (
ADDCOLUMNS ( Table1, "Year", YEAR ( Table1[Date] ) ),
Table1[ClientNo],
[Year],
"MaxDate", MAX ( Table1[Date] )
)
RETURN
SELECTCOLUMNS (
MaxDates,
"ClientNo", [ClientNo],
"Year", [Year],
"Type", LOOKUPVALUE (
Table1[Type],
Table1[ClientNo], [ClientNo],
Table1[Date], [MaxDate]
)
)
In calculating the variable, we add a Year column and then calculate the maximal date corresponding to that year.
Then we take that table variable, pick out the ClientNo and Year columns, and look up what the Type corresponding to the MaxDate.
If Note: you want to keep the MaxDate column, replace
[...] SELECTCOLUMNS ( MaxDates, "ClientNo", [ClientNo], "Year", [Year], [...]
with
[...] ADDCOLUMNS ( MaxDates, [...]

Using TREATAS to get Measure from Another Table

I'm back with another issue. I have a sales table with transaction details on the products purchased. I also have a table with warehouse inventory information for each product. I'm trying to get the count of products purchased in a Table visualization with columns from the warehouse inventory table.
I tried both of the measures below, but they both return the total Count for each row rather than sliced by product. Any help would be greatly appreciated!
NumProductsfromSales1 = calculate([Count], treatas(values('Sales'[Product]), 'Inventory'[Product]))
NumProductsfromSales2 =
var lineage = treatas(values('Sales'[Product]), 'Inventory'[Product])
var tbl = calculatetable('Inventory Detail', KEEPFILTERS(lineage))
var result = calculate(sumx(tbl, [Count]))
return result
From this source, we see TREATAS works as follows.
[Filtered Measure] :=
CALCULATE (
<target_measure>,
TREATAS (
VALUES ( <lookup_granularity_column> ),
<target_granularity_column>
)
)
is equivalent to
[Filtered Measure] :=
CALCULATE (
<target_measure>,
INTERSECT (
ALL ( <target_granularity_column> ),
VALUES ( <lookup_granularity_column> )
)
)
The important part is the ALL function. That's why you're losing the filter context from the rows in the visual.
I'm not sure if this is the most efficient solution, but I think if you add Inventory as a filter table to your first attempt, it should maintain the filter context on that table from the row in the visual.
NumProductsfromSales1 =
CALCULATE (
[Count],
'Inventory',
TREATAS ( VALUES ( 'Sales'[Product] ), 'Inventory'[Product] )
)
Edit: Regarding your comment, try the following:
a =
VAR top5prod =
SELECTCOLUMNS (
TOPN (
5,
SUMMARIZE ( Sales, Sales[Product], "Count", [Product Count] ),
[Count]
),
"Product", Sales[Product]
)
RETURN
CALCULATE (
[Product Count],
FILTER ( 'Inventory', 'Inventory'[Product] IN top5prod )
)
Using FILTER isn't as efficient as TREATAS but see if it works.
It's very difficult to answer this sort of question without having anything reproducible to work with.