Aggregate (Mean) of a resulting visual in Power BI - powerbi

I am having a problem since I want an aggregated version of a filtered resulting visual.
Process:
Using a Text Filter:
a. Input Merchant ID and Year-Month.
Resulting Visual 1 will look like this if you input Merchant ID (1234) and Year-Month (2020-01):
Merchant ID CardNum Year-Month Amount
1234 1abc1 2020-01 1.00
1234 2def2 2020-01 100.00
1234 3ghi3 2020-01 65.00
Visual 2 then displays the Merchant History for each CardNumber listed in Visual 1.
Resulting Visual 2 will look like this:
Card Number Merchant ID Date Amount
1abc1 abc 2020-01 1.00
1abc1 def 2020-01 2.00
1abc1 xyz 2020-01 3.00
2def2 abc 2020-01 100.00
2def2 xyz 2020-01 200.00
3ghi3 abc 2020-01 300.00
Now I want something that will give me this output ordered from highest to lowest:
Merchant ID xRatio
abc 1
xyz 2/3
def 1/3
xRatio is the mean of Merchant ID occurrences aggregated over the card numbers
Detailed Explanation:
Merchant ID abc occurred for all 3 card numbers thus ((1+1+1)/3) is the result
Merchant ID xyz occurred for 2 card numbers thus ((1+1)/3) is the result
Merchant ID def occurred for 1 card number only thus (1/3) is the result
where "n" is the count of unique card numbers as seen in Visual 1

Create this following 3 measure and 1 custom column in given order in your table-
1.
distinct_card_number =
CALCULATE(
DISTINCTCOUNT(your_table_name[Card Number]),
ALL(your_table_name)
)
2. Create a custom column as below
card-merchant = your_table_name[Card Number] & "-" & your_table_name[Merchant ID]
3.
distinct_merchant_count = DISTINCTCOUNT(your_table_name[card-merchant])
4. now create the final measure as below-
xRatio =
IF(
[distinct_card_number] = [distinct_merchant_count],
"1",
[distinct_merchant_count] &"/"& [distinct_card_number]
)
Now add merchant_id and xRatio to your table visual. The output will be like below-

Related

Is there any query string that I can use with the QUERY function to get a group-wise maximum?

I know how to use the GROUP BY clause in the QUERY function with either a single or multiple fields. This can return the single row per grouping with the maximum value for one of the fields.
This page explains it nicely using these queries and image:
=query({A2:B10},"Select Col1,min(Col2) group by Col1",1)
=query({A14:C22},"Select Col1,Col2,min(Col3) group by Col1,Col2",1)
However, what if I only want a query that returns the corresponding values for the most recent row, grouped by multiple fields? Is there a query that can do this?
Example
Source Table
created_at
first_name
last_name
email
address
city
st
zip
amount
4/12/2022 19:15:00
Ava
Anderson
ava#domain.com
123 Main St
Anytown
IL
12345
1.00
8/30/2022 21:38:00
Brooklyn
Brown
bb#domain.com
234 Lake Rd
Baytown
CA
54321
2.00
2/12/2022 16:58:00
Ava
Anderson
ava#new.com
123 Main St
Anytown
IL
12345
3.00
4/28/2022 01:41:00
Brooklyn
Brown
brook#acme.com
456 Ace Ave
Bigtown
NY
23456
4.00
5/03/2022 17:10:00
Brooklyn
Brown
bb#domain.com
234 Lake Rd
Baytown
CA
54321
5.00
Desired Query Result
Group by first_name, last_name, address, city, st, and zip, but return the created_at, email, and amount for the maximum (most recent) value of created_at:
created_at
first_name
last_name
email
address
city
st
zip
amount
4/12/2022 19:15:00
Ava
Anderson
ava#domain.com
123 Main St
Anytown
IL
12345
1.00
8/30/2022 21:38:00
Brooklyn
Brown
bb#domain.com
234 Lake Rd
Baytown
CA
54321
2.00
4/28/2022 01:41:00
Brooklyn
Brown
brook#acme.com
456 Ace Ave
Bigtown
NY
23456
4.00
Is such a query possible in Google Sheets?
Use this formula
=QUERY({QUERY(A1:I, " Select max(A),min(B),min(C),min(D),min(E),min(F),min(G),min(H),min(I) Group by B,C,E,F,G,H ", 1)},
" Select * Where Col1 is Not null ")
I believe that this is the formula you need:
=ARRAY_CONSTRAIN(SORTN(SORT(
QUERY({A1:I9,INDEX(IFERROR(REGEXEXTRACT(D1:D9,"(\D+)#")))},
"where Col2 is not null"),
10,1,1,0),9^9,2,10,1),9^9,9)
(Do adjust the formula according to your ranges and locale)
For the formula to work we create the helper column
INDEX(IFERROR(REGEXEXTRACT(D1:D9,"(\D+)#"))).
We also use 9^9 which equals to 387420489 rows, making sure that all rows are included in our sorting calculations.
Finally in our ARRAY_CONSTRAIN function we return the first 9 columns discarding the 10th helper column.
Functions used:
REGEXEXTRACT
IFERROR
INDEX
QUERY
SORT
SORTN
ARRAY_CONSTRAIN

How to create a visual showing top 3 employees with highest sales for every date in a bar chart?

I have a table with the Date, Analyst Name, and Quantity of tickets resolved. What I want to create is a visual with a stacked bar chart for every date showing the top 3 analysts with the highest quantities of tickets resolved for each day.
I have tried using the TOP N filter but it filters every analyst except the 3 with the highest number of tickets resolved overall. But I want for each date.
You could use a rank measure.
Considering the following table with sample data:
Date
Analyst Name
Quantity
2021-01-01
A
5
2021-01-01
B
10
2021-01-01
C
15
2021-01-01
D
20
2021-01-02
A
42
2021-01-02
B
32
2021-01-02
C
22
2021-01-02
D
12
2021-01-03
A
15
2021-01-03
B
5
2021-01-03
C
21
2021-01-03
D
30
You can write a measure like this:
Rank =
RANKX (
CALCULATETABLE (
'Table',
-- remove the filter an Analyst Name to only keep grouping by Date
REMOVEFILTERS ( 'Table'[Analyst Name] )
),
-- CALCULATE needed here for context transition
CALCULATE (
SUM ( 'Table'[Quantity] )
)
)
In your report just pull this measure into the filter panel and configure it like this:
The result will then look like this:

DAX Grouping and Ranking in Calculated Columns

My raw data stops at sales - looking for some DAX help adding the last two as calculated columns.
customer_id order_id order_date sales total_sales_by_customer total_sales_customer_rank
------------- ---------- ------------ ------- ------------------------- ---------------------------
BM 1 9/2/2014 476 550 1
BM 2 10/27/2016 25 550 1
BM 3 9/30/2014 49 550 1
RA 4 12/18/2017 47 525 3
RA 5 9/7/2017 478 525 3
RS 6 7/5/2015 5 5 other
JH 7 5/12/2017 6 6 other
AG 8 9/7/2015 7 7 other
SP 9 5/19/2017 26 546 2
SP 10 8/16/2015 520 546 2
Lets start with total sales by customer:
total_sales_by_customer =
var custID = orders[customer_id]
return CALCULATE(SUM(orders[sales], FILTER(orders, custID = orders[customer_id]))
first we get the custID, filter the orders table on this ID and sum it together per customer.
Next the ranking:
total_sales_customer_rank =
var rankMe = RANKX(orders, orders[total_sales_by_customer],,,Dense)
return if (rankMe > 3, "other", CONVERT(rankMe, STRING))
We get the rank per cust sales (gotten from first column), if it is bigger than 3, replace by "other"
On your first question: DAX is not like a programming language. Each row is assessed individual. Lets go with your first row: your custID will be "BM".
Next we calculate the sum of all the sales. We filter the whole table on the custID and sum this together. So in the filter we have actualty only 3 rows!
This is repeated for each row, seems slow but I only told this so you can understand the result you are getting back. In reality there is clever logic to return data fast.
What you want to do "Orders[Customer ID]=Orders[Customer ID]" is not possible because your Orders[Customer ID] is within the filter and will run with the rows..
var custid = VALUES(Orders[Customer ID]) Values is returning a single column table, you can not use this in a filter because you are then comparing a cell value with a table.

Informatica report query

Please find the below requirement,i tried with route and a sort and aggregator but amnt getting the actual output
Source records
ID type amount desc
1 Credit 300 xyz
2 Debit 100 abc
1 Credit 400 xyz
1 Debit 500 xyz
1 Debit 200 xyz
2 Credit 200 abc
Result
1 Credit 300 xyz
1 Credit 400 xyz
1 Debit 700 ( total of 1 credit & des is null)
1 Debit 500 abc
1 Debit 200 abc
1 Credit 700
2 Credit 200 abc
2 Debit 200
2 Debit 100 abc
2 Credit 100
You could create a mapping with 2 branches:
The first branch simply passes the records without any change to data
The second branch aggregates amounts grouping by ID and Type
You can then put a Union to get all records together, then a Sorter on the same keys, ID and Type, to get data in the right order for the Target.
The mapping would look like this:
Source -> Expression -> Union -> Sorter -> Target
+-> Aggregator --^

DAX formula to calculate account balance

I am trying to use DAX to calculate the balance on claims. I have the following tables in Power BI:
Claims
ClaimID Charge
123 $100.00
456 $250.00
789 $400.00
Payments
PaymentID ClaimID Payment
1 123 $20.00
2 123 $10.00
3 456 $50.00
4 789 $25.00
5 789 $25.00
6 789 $25.00
So I want to add a Measure to my Claims table and it should show the balance. Thus, 123 would have $70 ($100 - $20 - $10), 456 would have $200, and 789 would have $325.
Create the following measure in the Claims table:
Balance =
SUM ( [Charge] )
- CALCULATE (
SUM ( Payments[Payment] ),
FILTER ( Payments, [ClaimID] = MAX ( Payments[ClaimID] ) )
)
If ClaimID is not an aggregable column you can use FIRSTNONBLANK(Payments[ClaimID],0) instead of MAX ( Payments[ClaimID] ).
You will get:
Let me know if this helps.