SQL statement with where clause subquery to DAX expression - powerbi

I have a query written in SQL that I want to convert to DAX.
SELECT DISTINCT TeamId
FROM Teams
WHERE UId NOT IN
(
SELECT UId
FROM Items
)
AND IsArchived = 0
The power bi data model relationship with Teams and Items is One-to-Many (Teams to Items).
How can I convert the above SQL to DAX.

I found the solution to the task.
SELECT DISTINCT TeamId
FROM Teams
WHERE TeamId NOT IN
(
SELECT TeamId
FROM Items
)
AND IsArchived = 0
I used EXCEPT function in DAX.
EXCEPT(VALUES('Teams'[TeamId]), VALUES(DriveItems[TeamId]))
This returns distinct TeamID as a table

Related

how do i join two tables using dax

i have two tables in power bi that i am trying to join or lookup.
the period table has the days, months, and month id.
the data table has the days and data.
i need to pull the month id from the period table into the data table so that i have a table like the final result picture where the month id is now included.
final result
i tried using lookup and crossjoin but they dont work.
Month = EVALUATE
ADDCOLUMNS (
Sales,
"month", RELATED ( 'period'[day] )
)

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]
)

Power BI Visual Filter Not Filtering All Other Visuals

I have a date table that is used to populate a date slicer. The date slicer filters all other filters on the page but one. The one it doesn't filter has a one-to-many relationship back to the date table.
However, when I use the data from table two as an axis (Date and Hour) it actually displays all date/hours from the entire table but doesn't restrict the date/hour range to that of the parent date table (table one). Thoughts on how I can achieve this without using merge tables (preferable in DAX)?
In SQL Server I would do the following to achieve this output:
select fc1.calendardatewithtime, totaltable.total
from FiscalCalendarWithTime fc1
cross apply (
select top(1) count(distinct id) total
from ActionDetail ad1
where ad1.upgraded_on=fc1.calendardatewithtime and ad1.status=3
)as totaltable
where exists ( select 1 from dbo.FiscalCalendarTable fc2 where fc2.calendardate=fc1.calendardate and fc2.fiscalweek=1 )
order by fc1.calendardatewithtime asc;
Where calendardatewithtime is the date with time field that I would use as the axis and totaltable.total is the value I would display as the graph total.
My Date Slicer code:
SpecialDateDropdown =
VAR _datetable = FiscalDGCalendar
VAR _today = TODAY()
VAR _yesterday = TODAY()-1
VAR CurrentFiscalWeek = calculate(min(FiscalCalendar[fiscal_week]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
VAR CurrentFiscalPeriod = calculate(min(FiscalCalendar[fiscal_period]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
VAR CurrentFiscalQuarter = calculate(min(FiscalCalendar[fiscal_quarter]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
VAR CurrentFiscalYear = calculate(min(FiscalCalendar[fiscal_year]),filter(FiscalCalendar,format(now(),"mm/dd/yyyy")=format(FiscalCalendar[fiscal_date],"mm/dd/yyyy")))
RETURN UNION(
ADDCOLUMNS(FILTER(_datetable,[fiscal_date]=_today),"Period","Today","Order",1),
ADDCOLUMNS(FILTER(_datetable,[fiscal_date]=_yesterday),"Period","Yesterday","Order",2),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_week]=CurrentFiscalWeek),"Period","Current Fiscal Week","Order",3),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_week]=CurrentFiscalWeek-1),"Period","Prior Fiscal Week","Order",4),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_period]=CurrentFiscalPeriod),"Period","Current Fiscal Month","Order",5),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_period]=CurrentFiscalPeriod-1),"Period","Prior Fiscal Month","Order",6),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear&&[fiscal_quarter]=CurrentFiscalQuarter),"Period","Current Fiscal Quarter","Order",7),
ADDCOLUMNS(FILTER(_datetable,[fiscal_year]=CurrentFiscalYear),"Period","Current Fiscal Year","Order",8),
ADDCOLUMNS(_datetable,"Period","Custom Date Range","Order",9)
)
And this is how I am gathering my totals so the only piece I am missing is how to visually display only a specific date range:
CALCULATE(
IF(
ISBLANK(DISTINCTCOUNT(ActionHistoryDetail[id])),0,DISTINCTCOUNT(ActionHistoryDetail[id])
),ActionHistoryDetail[status]=3,USERELATIONSHIP(FiscalCalendar[fiscal_date],fiscalcalendarwithtime[CalendarDate]),USERELATIONSHIP(fiscalcalendarwithtime[CalendarDateWithTime],ActionHistoryDetail[upgraded_on])
)
You can group the slicers in a single family, as all the slicers will have the same values(if values are the same). Then you can hide the second slicer behind the first slicer.
As you can see Date Table doesn't filter DATA TABLE as I haven't grouped them yet.
Now I got to View in the ribbon and select the sync slicers option and then go to Advanced Controls and group both of the slicers as A.
View >> Sync Slicers >> Advanced Options
Now as both of the slicers are grouped, when I sleect the Year from the Data Table slicer it filters the DATA TABLE table too.
Now just hide the other slicer.
If this answer helped you, then mark it as answer. Thanks.
You should check your relationships.
Make sure you have a date table mapping to your main table.

Sum based on two columns from another table

I've got this table named "A" and I want to fill the Sales SUM column with the sum of sales from another table group by date and country. The other table is named "B" and got also Date, Country and Sales columns however the number of dates and countries differ. I don't want to join this tables I would like to achieve this in DAX. Is it possible?
Yes you can do that with DAX and virtual relationship:
SumSales = calculate( sum('B'[Sales])
, TREATAS(SUMMARIZE('A','A'[Date],'A'[Country]), 'B'[Date],'B'[Country])
)

How to Use NOT IN Query in PowerBI DAX

I have imported Order table from SQL into PowerBI
Order Table has Data Like below.
ID OrderNo CustomerNo OrderDate
1 DC001 1001 2020-06-01
1 DC002 1002 2020-06-09
1 DC003 1003 2020-06-10
Note: I want to Execute below Query in PowerBI DAX
Select Count(Distinct CustomerNo)
From [order] where orderdate >= '2020-06-08' and orderdate <= '2020-06-14'
And CustomerNo
Not in (select CustomerNo from [order] where orderdate < '2020-06-08')
I have tried below code in DAX
MEASURE NOT IN =
VAR indexList =SELECTCOLUMNS (
FILTER('Order','Order'[OrderDate] > [RangeFromDate]),"Distict", DISTINCTCOUNT ('Order'[CustomerNo]))
RETURN
SUMMARIZE (
FILTER('Order',
NOT ('Order'[CustomerNo]) IN indexList),
"Count",Count ( 'Order'[CustomerNo] )
)
Note: [RangeFromDate] is MEASURE dynamically load From date from the slicer.
But Not Working for me.
Kindly Help me to solve this in PowerBI DAX
The EXCEPT function can help with this. It will accept two tables as input, taking the values from the first table, and removing those from the second. I've used a summarize function to ensure each of those tables are distinct (the second table also includes the filter from your SQL subquery). Since they are distinct, I can just COUNTROWS of the resulting table to get the customer count.
RemainingCustomers =
COUNTROWS(
EXCEPT(
SUMMARIZE(Orders, [CustomerNo]),
SUMMARIZE(FILTER(Orders, Orders[OrderDate] < DATEVALUE("2020-06-08")) , [CustomerNo])
)
)