Power BI counting double values - powerbi

I'm trying to create a measure in Power BI that will count doubles as a single value and then later add them all up to see how many doubles we have. Here is an example:
Each customer whose name shows up more than once should be counted as 1
Bonus question, how can I make a measure which will count customers whose name only shows up once (example name - Sarah).
Thanks in advance!

If you want to count the customers distinct you can use:
CountCustomers = DISTINCTCOUNT([Customer])
if you want to count the doubles, you can use:
Doubles = COUNTROWS(FILTER(SUMMARIZE(CusTable, CusTable[Customer], "countC", COUNTROWS(CusTable)), [countCol] > 1))
First I summarize it to a table with the name of the customer and how often it is appearing in the table
Next I filter this table by all rows bigger than 1
Last I count the rows

You can create this below measure to check the customer is there for once or multiple time in the list. This measure will return 1 if the customer is there for once and 0 if the customer exists for multiple time.
is_unique =
VAR current_customer = MIN(your_table_name[customer])
VAR customer_count =
CALCULATE(
COUNT(your_table_name[customer]),
FILTER(
ALL(your_table_name),
your_table_name[customer] = current_customer
)
)
RETURN IF(customer_count = 1, 1, 0)
This will return 1 for customer- Sarah and David. For all other customer, it will return 0. Now, if you add the above measure to a Card and apply SUM on the measure, it will return value 2 which is basically your customer count with single existence.

Related

How do I create a quarter-over-quarter variance column or measure with a table of account balance totals by quarter?

I have a table that lists quarter-end account balances by account and business group and I'd like to create a measure or column that shows the quarter-over-quarter balance change for each company/account combo (e.g., Group A, Account 10000 quarter-over-quarter change is $500).
I've tried various formulas using the calculate function, but these don't work if I don't apply aggregation (SUM and SUMX are what I've tried) to the AMOUNT column. The aggregations seem to mess up the results. I either get blank values or duplicate amount values. I think it's because the amounts don't need to be aggregated. They're already aggregated values.
How can I do a simple subtraction based on quarterly dates without having to aggregate my amount column?
The formula(and similar variations) I've tried:
QtrChange =
VAR CurrentAmt = TABLE1[AMOUNT]
VAR PrevAmt = CALCULATE(SUM(TABLE1[AMOUNT]), PREVIOUSQUARTER(TABLE1[DATE].[DATE])
RETURN = CurrentAmt - PrevAmt
Table example:
Business Group
Account
Quarter end balance
Quarter
A
100000
$2000
3/31/22
A
200000
$3000
3/31/22
B
100000
$4000
3/31/22
B
200000
$5000
3/31/22
Edit:
Edited DAX formula with labels tying to example table, for better clarity:
QtrChange =
VAR CurrentAmt = TABLE1[Quarter end balance]
VAR PrevAmt = CALCULATE(SUM(TABLE1[Quarter end balance]), PREVIOUSQUARTER(TABLE1[Quarter].[DATE])
RETURN = CurrentAmt - PrevAmt

Value of the first order of every customer

I'm working on a school assignment where I have to look at the value of every first voucher value of a customer. Now I am working on it and I did manage to get the first date of each customer. However, when I put the value in the table it always takes the total sometimes of this customer ID so that some lines score considerably higher
Code
First transaction date =
MINX(FILTER('Tekst','Tekst'[PCHN]='Tekst'[PCHN]),'Tekst'[Datum].[Date])
enter image description here
Try making your first transaction date into a column:
First_transaction_date = CALCULATE(
MIN('Table'[date]),
FILTER(
'Table',
'Table'[customer]=EARLIER('Table'[customer])
)
)
Then calculate the corresponding value. I used SUM, so if multiple values are on the same date, it will sum them. If you want the min/max/mean of these values when multiple occur, that's up to you:
first_voucher_value = CALCULATE(
SUM('Table'[value]),
'Table'[First_transaction_date] == 'Table'[date])
I believe in your example:
Table => Tekst,
date = Date,
customer = PCHN,
value should be your 'voucher value column'.

Power BI: Count numbers by Today, and the count numbers of the previous years at the same date

I'm new to Power BI and DAX.
I want to show the total ID count of this year and the total ID counts of previous years at the same dates each of which is associated with a different Category_Number.
The below tables show the original data set (1st table) and the result table (2nd table) I would like to have.
enter image description here
enter image description here
Any ideas or suggestions will be greatly appreciated.
Thanks!
You want to filter and use SAMEPERIODLASTYEAR.
To get the values of one category you can do:
Category_Nbr 80 = CALCULATE(COUNTROWS(myTable),myTable[Category_Nbr] = 80)
for the value of this but for the previous year you could use your DATE TABLE if you have it, if not, use the automatic one. I'll assume you don't have one so use the automatic.
Category_Nbr 80 LY = CALCULATE([Category_Nbr 80],SAMEPERIODLASTYEAR(myTable[Effective_Date].Date))
With this you can do a comparison over time
Category_Nbr Count YoY = DIVIDE([Category_Nbr 80 LY]-[Category_Nbr 80],[Category_Nbr 80 LY])
You can just adapt the filter for other categories.
Consider having a look into Quick Measures really nice way to start learning DAX.
Below are two measures I created.
The Total_ID measure works well. It shows the total number of IDs that have the Category_Number =80 and Effecitve_Date < Today.
The Total_ID_LY measure shows the total number IDs that have the Category_Number = 70, but doesn't show the Effective_Date< The same data last year.
I want the Total_ID_LY measure to have two filters, Category_Number = 70 (80-10) and Effective_Date < The same data last year of today.
Any helps?
Thanks!
Total_ID = COUNT('myTable'[ID])
Total_ID_LY =
VAR CurrentCategory = SELECTEDVALUE ( 'myTable'[Category_Number] )
VAR PreviousCategory =
CALCULATE (
MAX ( 'myTable'[Category_Number] ),
ALLSELECTED ( 'myTable' ),
KEEPFILTERS ( 'myTable'[Category_Number] < CurrentCategory )
)
VAR Result =
CALCULATE (
[Total_ID],
'myTable'[Category_Number] = PreviousCategory
)
RETURN
Result

Create calculated measure from other table with filters

I have four tables which I have tried related and unrelated:
Store (column "Store Number")
Calendar (column "Sales Date")
SKU (column "SKU Code")
Sales (columns "Store Number", "Sales Date",
"SKU Code" and "Sales Quantity")
I have slicers on the Calendar and SKU tables
I need to list all stores with total "Sales Quantity" for each store and at the same time to limit the sales quantity to the two slicers mentioned above. Basically, I need to list these columns:
Store Number - from the Store table (no filtering from Slicers)
Store Name - from the Store table (no filtering from Slicers)
Total Quantity of Sales for the Store - calculated measure filtered by Calendar and SKU slicers
So my question is, what DAX required to create the calculated measure?
Please note I must list ALL stores regardless of whether they have sales in the stipulated period.
I've tried various DAX functions such as TREATAS, SUMMARIZE, ETC.
I've tried with and without active relationships and with no relationships.
The closest I've got is the code below, but it excludes stores with zero sales. I need all stores regardless of their sales.
Qty by Store = CALCULATE(
sum(Sales[Sales Qty])
,USERELATIONSHIP(
Sales[Store Number]
,Store[Store Number]
)
)
The problem with the output I've managed is that stores without sales are excluded from the list. I need to have them included.
Keep the relationship active, and change the DAX formula to
Qty by Store =
VAR res = sum(Sales[Sales Quantity])
RETURN IF (ISBLANK(res), 0, res)
There is no need for USERELATIONSHIP(). Relationship Store - Sales is already active. The reason why the number of stores changes in the table visual is because when there is no sale for a particular store Qty by store measure returns BLANK and those BLANKs get filtered out by the table.
Result:
An easy way to make a blank return zero instead is to simply append +0 to your measure formula.
Qty by Store = SUM ( Sales[Sates Quantity] ) + 0
This works because DAX calculates BLANK() + 0 = 0.

Find Max for each day in PowerBI and DAX

I am trying to find the maximum route for each day based on the count of cars in PowerBI/DAX.
An example of my data is as follows:
Date Route Count
01-Nov-17 A 10
01-Nov-17 B 5
02-Nov-17 A 2
02-Nov-17 C 22
03-Nov-17 B 2
Hence I want to find the max count of route for each date and display the results of a table like so...
Date Route Count
01-Nov-17 A 10
02-Nov-17 C 22
03-Nov-17 B 2
Any suggestions would be very much appreciated.
Thanks,
Fiona
First, define measure for max car count:
[Max Count] = MAX( Cars[Count] )
If you drop this measure into a pivot against dates, it will show you max car counts per date.
Define second measure:
[Max Routes] =
VAR Period_Max_Count = [Max Count]
RETURN
CONCATENATEX (
FILTER ( Cars, [Max Count] = Period_Max_Count ),
Cars[Route], ","
)
This measure will return a string of routes per date that have max count. You need a list instead of one value because of potential ties - multiple routes might have the same max count per period. It's not in your data example, but just to demonstrate this, I added an extra record for the first date:
The way this measure works:
first, it saves max car count per date into a variable.
second, it filters car table to select only routes that have count equal to max count for the date.
third, it iterates over the filtered table and concatenates route names into a list separated by comma.
Right-click the table choose New quick measure
In Calculation drop-down select Max per category
In Base value field, drag the Count column. In this, the value will be aggregated to Sum by default so instead change it to Max of Count
In Category field, drag the route column
Voila! Magic happens! The measure that is created, when plotted against the axis Route will give Max(Count) per Route.
Here is what the DAX will look like:
Count max per route =
MAXX(
KEEPFILTERS(VALUES('Table1'[route])),
CALCULATE(MAX('Table1'[Count]))
)
(so one can directly use this DAX without wanting to drag but i dont understand the DAX at this moment tbh)
Lucky reference for me:
https://learn.microsoft.com/en-us/power-bi/desktop-quick-measures
Create a calculated column with formula:
MAX = IF(CALCULATE(
MAX(Table1[Count]);
FILTER(
Table1;
Table1[Date] = EARLIER(Table1[Date])
)
) = Table1[Count]; Table1[Route]; BLANK())
Create your table and make a page level filter to show all non-blank values of Table1[MAX].