How to group by on Power BI using DAX - powerbi

I have two tables in Power BI as follows:
COUNTRIES
COD COUNTRY
1 BRAZIL
2 ARGENTINA
3 CHILE
4 BRASIL
5 COLOMBIA
6 ARGENTINA
7 URUGUAI
SALES
COD DATE
1 2021-01-02
2 2021-10-01
3 2019-09-04
1 2018-07-05
7 2019-04-10
There's a relationship between the two tables, on the COD column.
I need to count how many countries (column "COUNTRY" from the table "COUNTRIES") have a status CHURN. It's considered CHURN when their latest order from the table "SALES" is more than 180 days, using today as a reference.
I know that I need to group by the MAX date per country, do a DATEDIFF, and then do a COUNT. I've tried using ALL and SUMMARIZE, but I haven't found a way to solve this problem.

Are you able to add a calculated column to store the max sales date for each country in your COUNTRIES table? Either in Power BI or directly in your database. If so, here's one solution with 2 steps.
Create a MaxSalesDate column in your COUNTRIES table. DAX for a calculated column below:
MaxSalesDate =
VAR COD = COUNTRIES[COD]
RETURN MAXX(FILTER(SALES, SALES[COD] = COD), SALES[DATE])
Create a measure that counts the number of MaxSalesDate values that are older than 180 days old:
CountCHURN = COUNTX(COUNTRIES, IF(DATEDIFF(COUNTRIES[MaxSalesDate], TODAY(), Day) > 180, 1))

Related

Power BI translating a sql query to filters

I was wondering if this is possible in Power BI, I am extremely new to this and I am trying to relate how a sql query can translate in to a power bi report.
SELECT
expiresDate,
Name,
Addr,
ValidFrom,
ValidTo,
ChildName,
ChildValidFrom,
ChildValidTo,
RecValidFrom,
RecValidTo
FROM Table
WHERE expiresDate Between <date1> and <date2>
AND <Date3> BETWEEN ValidFrom AND ValidTo
AND <Date3> BETWEEN ValidFrom AND ValidTo
AND <Date3> BETWEEN ValidFrom AND ValidTo
A brief explanation. The report is for 3 months in advance. So in August the report is for September <date1 = 01/09/2021) and October (date2 = 31/10/2021) data. However the data can change on a daily basis. So this depends on Date3 which could be any day in August.
I have created a table that is a calendar and has the additional columns that calculate the start and end dates from a particular date. I just can't work out how to relate this to the dataset which is the query without the WHERE. I would then want the filters to be able to determine the result. Ultimately as I have it at present a single date that will then get the dates from the start and end dates as described earlier. Or display by range using the latest iteration of the record to display.
For example, First part of table
expiresDate
AccNo
Name
Addr
ValidFrom
ValidTo
ChildName
2021-10-01
1
Robert
1 Here
2019-01-01
2021-08-16
Cheese
2021-10-01
1
Robert
1 Here
2019-01-01
2021-08-16
Rhubarb
2021-10-01
1
Bob
1 Here
2021-08-17
2020-08-23
Rhubarb
Second half of table
ChildValidFrom
ChildValidTo
RecValidFrom
RecValidTo
2019-01-01
2021-08-10
2019-19-01
2020-12-31
2021-08-11
2021-08-23
2021-01-01
2021-08-15
2021-08-11
2021-08-23
2021-08-16
2020-08-23
The table is a view which has squashed the data to unique records and when the changes occurred. The dataset is considerably lower, a record count from 10m to 54k.
The requirement is that all To - From dates are within the date specified. Either being a date in the calendar that is entered as a filter... or today.
The report would bring out all records that have an expiryDate greater than 1 calendar month of the date, and less than 3 calendar months. I am just using August dates for the example so this would be from the 01/09/2021 - 31/10/2021.
If I use date 2021-08-01.
In my example there are 3 results for AccNo 1, but Only 1 should be displayed.
If I use the date 2021-08-01 the first row would be displayed.
If I use the date 2021-08-12 the second row should displayed.
If I use the date 2021-08-23 the third row should displayed.
Because the date used should fall between the date range of all 3 criteria
ValidFrom - ChildValidTo
ChildValidFrom - ChildValidTo
RecValidFrom - RecValidTo
Any help would be greatly appreciated. This is extremely frustrating, but I can understand that if this is possibly that this would make a nice visual for the users to check through their data based on entering a date.
Many thanks

How to remove or exclude rows from a table using date slicer selection?

As per the below scenario,I need to exclude all the rows in the table Sales with columns Client,sales,salesdate and region which fall outside the selected maximum and minimum date range of the date slider.
Could anyone please suggest a DAX , I will share my DAX query which is not working.I tried to search official guides in PowerBI for selectedValues() to handle this date filter condition but nothing seems to be working.
I created a Calendar table with distinct values of the dates as present in the Sales Dates column of the Sales table. There is no relationship between these two tables. I used the below DAX measures to handle the filtering criteria for excluding the rows:-
Below measures are meant to find maximum and minimum dates in the selection slider
MaxDate=MAX(DateDimention[Dates])
MinDate=MIN(DateDimention[Dates])
Below ExcludedMeasure is meant to set 1 flag to the values other than selected values which are selected for both min and max dates.
I dragged and dropped it in the visual filter of my table Sales and set it to dont show.But it seems to be not working.
Please suggest how can I fix it.
ExcludeMeasure = IF (SELECTEDVALUE(Calendar,Calendar[MinDate],0,1),
IF (SELECTEDVALUE(Calendar,Calendar[MaxDate],0,1)
My Present Visualization and independent Calendar table to be used in the filter slider:-
Present visualization
Independent Calendar Table
Input data source [Excel]
Client Sales SalesDates Region
A 1000 1.1.2000 USA
A 100 1.2.2000 USA
A 200 4.3.2000 USA
B 110 4.3.2000 Europe
B 1000 5.4.2000 Europe
B 200 6.8.2001 Europe
C 1100 7.9.2001 Asia
C 2000 8.12.2001 Asia
D 100 1.2.2002 Australia
D 1300 6.3.2002 Australia
E 100 7.5.2002 Africa
Expected Results :
If I select Minimum slider date as 01.04.2001 and maximum slider date as 1.09.2001 in my[Dates] filter visualization, then it should return the below results after excluding '5.4.2001' and '6.8.2001':-
Client Sales SalesDates Region
A 1000 1.1.2000 USA
A 100 1.2.2000 USA
A 200 4.3.2000 USA
B 110 4.3.2000 Europe
C 1100 7.9.2001 Asia
C 2000 8.12.2001 Asia
D 100 1.2.2002 Australia
D 1300 6.3.2002 Australia
E 100 7.5.2002 Africa
Kind regards
Sameer
A relationship between the date table and the sales table would be ideal, as when you filter the date from the date table, the filter would exclude the records from the sale tables.
If you can't have a date table with the relationship, your exclude measure needs to be something like this:
Exclude =
CALCULATE(COUNT(Sales[Id]),
Sales[SalesDates] >= [MinDate] &&
Sales[SalesDate] <= [MaxDate]) > 0

MDX query for percent of current row within table

I have an SSAS cube with a FactTicket, that has a TicketsSold measure.
The cube has a good number of dimensions. Lets call them Date, Agent and Geo.
I can create a table (we're using Power BI service, if it makes a difference) and put in it all and any dimensions, and the TicketsSold measure.
What I'd like to do, is have another measure that'll calculate the percent of each row in the table out of the total of the table at that moment.
so suppose I have the following data:
Date Agent Geo TicketsSold
2020-04-01 Moulder US 12
2020-04-01 Moulder UK 4
2020-04-02 Moulder US 10
2020-04-03 Moulder UK 5
2020-04-01 Skully US 16
2020-04-01 Skully UK 12
I would like to be able to filter my report on any measure, display any other measure(s) with the TicketsSold and the percent.
Such as, filtered on 2020-04-01 and 2020-04-02:
Agent Geo TicketsSold %ofTicketsSold
Moulder US 22 40.74 %
Moulder UK 4 7.40 %
Skully US 16 29.63 %
Skully UK 12 22.22 %
TOTAL 54 100 %
Or filtered only on Agent Moulder:
Date TicketsSold %ofTicketsSold
2020-04-01 16 51.61 %
2020-04-02 10 32.26 %
2020-04-03 5 16.13 %
TOTAL 31 100 %
It's clear to me that what I probably need is some way of getting -- for each row in the table -- the total of the table itself, and then dividing the row's own TicketsSold by this total.
But I can't seem to figure out a trivial way of creating such a calculation. Is this doable, without having to define a tuple of all possible dimensions?
Thanks!
You can achive that using the axis function . Below is an example based on Adventureworks
where we will create a PercentageOFTotal Column based on InternetSales
select
{[Measures].[Internet Sales Amount]}
on columns,
non empty {([Product].[Category].[Category],[Product].[Subcategory].[Subcategory])}
on rows
from
[Adventure Works]
where [Date].[Calendar Year].&[2013]
Result
Now lets add our new column
with member measures.PercentageOFTotal
as round(([Measures].[Internet Sales Amount]/sum(Axis(1),[Measures].[Internet Sales Amount]))*100,2)
select
{[Measures].[Internet Sales Amount],measures.PercentageOFTotal }
on columns,
non empty {([Product].[Category].[Category],[Product].[Subcategory].[Subcategory])}
on rows
from
[Adventure Works]
where [Date].[Calendar Year].&[2013]
Result
If you are already using PBI: why don't you make use of DAX? A DAX query is able to solve your problem: MEASURE Fact[Percent] = CALCULATE(SUM(Fact[Amount]), ALLSELECTED(Fact)). "ALLSELECTED" is similar to VISUALTOTALS() in MDX, but more dynamic.

DAX Measure or Calculated Column from Slicer Value

I have a table with customers' SSN, account number, purchase date, and max purchase date (the most recent purchase date for SSN, across all the accounts). Customers can have multiple accounts. I know how to create a measure the calculate the distinct count of all the accounts that haven't been active since a certain date (6 months, 18 month, 24 months)..
I would like to create a measure or a calculated column to give me the following information.
when users select the date from the slicer (say 6 months) the chart shows the count of the accounts that have not made a purchase in 6 months, the users also want to have a drop down slicer("Yes", "No") to indicate if the SSN had activities under other accounts. i.e. if the max purchase date is greater than the value from the date slicer.
the table structure looks like this:
SSN AccountNumber LastPurchaseDate MaxPurchaseDate
123-45-5678 9876 8/2/2018 9/4/2019
123-45-5678 6398 9/4/2019 9/4/2019
135-65-4321 2233 6/6/2019 6/6/2019
Best way here would be if you add a custom column with the time difference (in the query designer):
= [MaxPurchaseDate] - [LastPurchaseDate]
Now you have something like this:
SSN AccountNumber LastPurchaseDate MaxPurchaseDate DateDiffDays
123-45-5678 9876 9/2/2018 9/4/2019 2
123-45-5678 6398 9/4/2019 9/4/2019 0
135-65-4321 2233 6/12/2019 6/6/2019 6
You can add another column which acts as filter for your 6 months, 18 month, 24 months (convert the DateDiffDays into months).
The following measure counts the accounts:
=Distinctcount('YourTable'[AccountNumber])
If you filter now by your 6 months, 18 month, 24 months column the measure gets after every filtering calculated again and you get your result.

Distinct count of rows using a join in Power BI

I want to get a distinct count of Enquiries with Food and Drink in Power Bi. The way the database is structured is:
1 Enquiry could have multiple days, which have food and drink attached to them.
I want to try and calculate how many enquiries have food and drink attached so I need to do a distinct count on the enquiry id in the Enquiries table where a record exists in the food and drink table
The table structure is as follows:
Enquiry Table
EnquiryId
10
Enquiry Day Table
EnquiryDayID EnquiryId
5 10
6 10
Enquiry Day Food Drink Table
EnquiryDayFoodDrinkId EnquiryDayId
20 5
21 6
Basically I want the distinct count to only return 1 as there is only one distinct enquiry with food + Drink attached.
Here is the code in SQL which return the correct number. I want to get the same result in Power BI
select count (distinct(e.pkEnquiries))
from EnquiryDayFoodDrink edfd
inner join EnquiryDay ed
on ed.EnquiryDayId = edfd.EnquiryDayId
inner join Enquiries e
on ed.EnquiryId = e.pkEnquiries