I have a list of currency trades, and from this I need to calculate the high and low points of the balance of each currency. I have created a simple example in the Sheet below:
https://docs.google.com/spreadsheets/d/1fxlfh-WBquyTR7wGKgHE3p2GV1zT9eSdrKmO9FJ3A8E/edit?usp=sharing
Here there are six trades involving three different currencies. Assuming that the balance of each currency is 0 before trade #1, I have manually calculated the balance high and lows in the table on the right for each of the three currencies.
How would I go about calculating these balance high and lows through a formula?
try:
=INDEX(QUERY({QUERY(FLATTEN({B2:B, D2:D}), "where Col1 is not null", ),
MMULT(--TRANSPOSE(IF((SEQUENCE(1, COUNTA(A2:A)*2)>=SEQUENCE(COUNTA(A2:A)*2))*(
QUERY(FLATTEN({B2:B, D2:D}), "where Col1 is not null", )=TRANSPOSE(
QUERY(FLATTEN({B2:B, D2:D}), "where Col1 is not null", ))),
QUERY(FLATTEN({C2:C, IF(E2:E="",,-E2:E)}), "where Col1 is not null", ), 0)),
SEQUENCE(COUNTA(A2:A)*2)^0)},
"select Col1,max(Col2),min(Col2)
group by Col1
label max(Col2)'Balance high',min(Col2)'Balance low'"))
Related
So, I have two tables, Scores and Accounts.
ID
Score
1
120
2
150
3
100
ID
Account
1
Account 1
2
Account 2
3
Account 3
I also have 4 measures that calculate the quartile percentile for all of the scores in the Scores table. I was wondering if it was possible to have a measure that concatenates the accounts into one line if their score is, for example, greater than the Quartile 1 measure. For example, if quartile 1 is 110, then I want a measure that would give me "Account 1, Account 2". Is this possible?
I managed to get your result by implementing the following measure, assuming you have a relationship between the two tables.
Accounts GT Q1 =
CONCATENATEX(
FILTER(
Scores,
Scores[Scores] > [Quartile 1]
),
RELATED( Accounts[Account] ),
", "
)
Output
There may be a simpler way. Let me know if that worked.
I need help calculating the percentage of each category in a column (based on the grand total) in DAX but for one specific category.
This is how the data is structured. Each row is an individual transaction with an ID column and item column.
I need to get the % of transactions that are for bagels only. This is my sql code I currently use.
`Select 100 - CAST(count([Items])
- count(case [Items] when 'Bagel' then 1 else null end) AS FLOAT)
/ count([Items]) * 100 as Percent_Bagel
from Items_Table where Items != 'Coffee'
and Items != 'Muffin'`
I need this to be converted to a DAX formula to use in a Power BI measure if possible, but I am new to DAX and don't know where to begin.
Thank you.
The "right" implementation for you always depends on the context. You can achieve your goal through different approaches.
I have used the following:
Measure =
DIVIDE(
-- Numerator: Filter all Bagel's transaction and count them
CALCULATE(
COUNT('Table'[Transaction ID]),
FILTER('Table', 'Table'[Item] = "Bagel")
),
-- Denominator: Remove any filter - essentially fixing the full table - and count all transactions we have
CALCULATE(
COUNT('Table'[Transaction ID]),
ALL('Table'[Item])
),
-- If something goes wrong with the DIVIDE, go for 0
0
)
You may also use the filters to rule out all measures that are not blank.
Without measure filter
With measure filter (Other categories are gone)
Hope this is what you are looking for!
I have a table with 15 people that each month get 7-day scores. I want to use the RANKX formula in Power BI to rank the lowest (1) to the highest average score.
This works fine if I look at all, but start to act weirdly when I use a slicer and only look at one or two months for example. The ranking doesn't start with 1 anymore?
I use this formula:
Rank = RANKX(
ALLSELECTED('Score Table'[Person]);CALCULATE(AVERAGE('Score Table'[Score]));;ASC;Dense)
Look at the image attached, please.
Help much appreciated image showing the issue
Can you try this and see if it works?
Rank =
RANKX(
CALCULATETABLE(
VALUES( 'Score Table'[Person] ),
ALLSELECTED( 'Score Table'[Person] )
),
CALCULATE( AVERAGE( 'Score Table'[Score] ) ),
,
ASC,
Dense
)
Let's think about the original code step by step.
It iterates over "Person 1" to "Person 20" and calculates the average score of that person.
Evaluate the average score of the person of current filter context (say "Person 1").
Find the ranking position of "Person 1" in 20 persons.
In the step (1), it includes all Persons from 1 to 20 because there is no Person filter in the visual. Here, it looks there is no scores of Person 15 and 18 in the selected period, so it evaluates to BLANK.
Now, the document of RANKX says,
If expression or value evaluates to BLANK it is treated as a 0 (zero) for all expressions that result in a number, or as an empty text for all text expressions.
The average scores of Person 8 and 15 are BLANK, so RANKX treats it as 0. Now going back to Person 1, her average score was 62.43, and there were two people with average score of 0. Therefore, the rank of Person 1 will be 2.
By wrapping 'Score Table'[Person] with VALUES inside CALCULATETABLE, you can omit persons who has no scores in the selected period.
I have a matrix that is using the fields "Product Group" and "Product" in rows.
I want to calculate the average price for the products that had sales last year. Sales LY is a calculated measure:
Sales LY = CALCULATE(SUM('Table'[Qty]);SAMEPERIODLASTYEAR('Calendar'[Date]))
And this is the measure I'm trying to find:
AvgPrice= CALCULATE(DIVIDE([Turnover];[Quantity]);FILTER('Table'; [Sales LY]>0))
This works fine on the lower granularity (Product), but on the higher granularity (Product group) the calculation is wrong because the Product group is adding ALL the products within that Product group and I only want the ones that had sales last year.
How do I tell DAX: "Use the lower granularity"?
One option for situations like this is to make the higher granularities a sum of the lowest granularity values. Something like this:
GroupAvgPrice = CALCULATE( SUMX( VALUES( 'Table'[Product] ), [AvgPrice] ) )
When there is only one product in the evaluation context, this simply reduces to [AvgPrice], but should work at higher granularities as well.
I have the following spreadsheets. I need to compact the contents of this spreadsheet. I was wondering if it is possible to group similar items such as 1952 and add the numbers in the next column and output something along 5m / 2w or similar? 1951 would be compacted to 9w. This data is constantly changing and new reference numbers are added often.
Sheets doesn't do well adding numbers when they're right next to letters, so you'll need to split those cells into a number column and letter column using left() and right(). Put the number from left() inside the value() function as well so Sheets knows it's a number.
Example sheet
Once you have the helper columns made, you can use query() to consolidate and sum up your values. Query language can get tricky so I recommend the reference page. Once you have the sums and letters spit out, you can concatenate them in another column (J for me).
=ARRAYFORMULA(SUBSTITUTE(TRIM(SPLIT(TRANSPOSE(QUERY(TRANSPOSE({
SORT(UNIQUE(INDIRECT("B2:B"&COUNTA(B2:B)+1)&"♦")),IF(ISNUMBER(
QUERY(QUERY(TO_TEXT(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(IFERROR({
B2:B&"♦", REGEXEXTRACT(C2:C, "\d+")*1, REGEXEXTRACT(C2:C, "\d+(.+)")}),
"select Col1,sum(Col2),Col3
where Col3 is not null
group by Col1,Col3
label sum(Col2)''", 0)),,999^99)), "♦")),
"select count(Col1)
group by Col1
pivot Col2", 0), "offset 1", 0)), SUBSTITUTE(
QUERY(QUERY(TO_TEXT(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(IFERROR({
B2:B&"♦", REGEXEXTRACT(C2:C, "\d+")*1, REGEXEXTRACT(C2:C, "\d+(.+)")}),
"select Col1,sum(Col2),Col3
where Col3 is not null
group by Col1,Col3
label sum(Col2)''", 0)),,999^99)), "♦")),
"select count(Col1)
group by Col1
pivot Col2", 0), "limit 0", 1), " ", ), )}),,999^99)), "♦")), " ", " / "))
spreadsheet demo