How to Use NOT IN Query in PowerBI DAX - powerbi

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

Related

Find number of orders for customer first month

I am new to Power BI and learning how to perform cohort analysis with DAX in Power BI. Is there any way to find out how many times (or if easier if a customer buys more than a certain threshold) their first month?
If I have the table Customers:
ID | DateOfFirstRegistration
And the table of orders:
ID | customerId | orderDate
Let me know if any more information is helpful!
EDIT: If possible, is it also possible to plot it in a matrix with customers in each row and month 1 through 3 in the columns?
Thank you
You can do something like this measure in a table visualization together with customer ID:
First Month Purchases =
VAR _registration =
SELECTEDVALUE ( 'customers'[DateOfFirstRegistration] )
RETURN
CALCULATE (
COUNTROWS ( 'orders' ) ,
DATESINPERIOD (
'orders'[orderDate] ,
_registration ,
1 ,
MONTH
)
)

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

How is measure able to filter dimension via fact table?

I have 2 tables: dimProduct and factSales
Dim table has productid, name, category
Fact table has salesid, productid, status, amount, paiddate
I have a table visual that shows the status from the fact table. Against each status I want to show the count of products and the count of distinct category.
CountProducts=DISTINCTCOUNT(FACTSALES[PRODUCTID])
CountDistinctCategory=DISTINCTCOUNT(dimProduct[category])
How to correct this?
Set the cross-filter direction for your relationship to 'Both', so that filtering can also propagate from the fact table to the dimension table.
Alternatively, using DAX:
CountDistinctCategory =
CALCULATE(
DISTINCTCOUNT( dimProduct[category] ),
TREATAS(
VALUES( factSales[productid] ),
dimProduct[productid]
)
)

Distinctcount entrys with date slicer

this is probably pretty simple but I couldn't find any solution.
I have 2 tables: 'DateTime' and 'Usage' and I am using a date slicer (from to) which refers to 'DateTime'[Date]. Now I want to DISTINCTCOUNT 'Usage'[LPNumber] but only the ones which have a date that is included by the slicer. The table 'Usage' does also have a column with dates: 'Usage'[ConnectionStart Day]
I tried this but getting an error:
ActiveLP =
VAR start_date =
MIN ( 'DateTime'[Date] )
VAR end_date =
MAX ( 'DateTime'[Date] )
RETURN
CALCULATE(
DISTINCTCOUNT( 'Usage'[LPNumber] );
FILTER(
'Usage';
'Usage'[ConnectionStart Day] >= start_date
&& 'Usage'[ConnectionStart Day] <= end_date
)
)
The error says: A circular dependency was detected: DateTime[different columns]
Can someone please help me? Thank you very much :)
Just create a realtionship between your DateTime table and your Usage table (by the date). Then use this simple measure:
Distinct Count = Distinctcount('Usage'[LPNumber])
If you put now a slicer (from the DateTime table) on your report and filter it, the other table also get filtered, because of the relationship. Thus the value of Distinct Count will change, according to your date slicer.

Power Bi Sorted Date Table

I have an identified date within a fact table and I need to create a new table that's formatted "MMM-YY" (JAN-01) and sorted in month order.
I entered the data manually from a Excel Spreadsheet with dates ranging from Jan-15 to Dec-30 and sorted by an id num but ideally I want it to update monthly based on a identified date column within the fact table in BI. So for now id want dates upto Sep-19 which would update when the identified dates are refreshed within the fact table.
For now I've used the below to format the column which was inserted manually however id like to change the monthref.date to be based off of an identified date within my fact table.
ID Date = FORMAT('Month Ref'[Date],"MMM-YY")
Id want my date within the fact table to be lookedup/refrenced in another table formatted "mmm-yy" and to be in order of calendar month.
I can use
CALENDAR(MIN(FactEfficiencies[identified_date].[Date]) AND MAX(FactEfficiencies[identified_date].[Date])) to get every date then format but it will bring back the month multiple times when I just need one.
You are already on the good direction with the calendar table. One thing that could help you achieve your result could be including a SUMMARIZE or GROUPBY function. Have a look at this example.
Calendar =
var _fullCalendar =
ADDCOLUMNS (
CALENDAR ( MIN ( 'Table'[Date] ) ; MAX ( 'Table'[Date] ) ) ;
"MonthYear" ; FORMAT ( [Date] ; "MMM-YY" )
)
RETURN
SUMMARIZE ( _fullCalendar ; [MonthYear] )