Max if statement based on column - if-statement

I have week no column that came from date, based on the max week no column I am trying to get the if statement yes or no.
Example: if week number column contain from 01 to 40 so 40 is the highest week no so I would like to get the result is Yes by using calculate column otherwise no.
Week No Deired Result
1 No
2 No
3 No
4 No
5 Yed

I got the answer...
IF(MAX(Table [weekno]) =Table [weekno], "Yes", "No".

Related

DAX Calcuate rolling sum

I have a problem with calculating measure that sums values for 3 previous periods.
Below I attach sample fact table and dict table to show problem I am facing.
date
customer
segment
value
01.01.2021
1
A
10
02.01.2021
1
A
10
03.01.2021
1
A
10
04.01.2021
1
A
10
01.01.2021
2
B
20
02.01.2021
2
B
30
03.01.2021
2
B
40
dict table:
segment
segment_desc
A
Name of A
B
Name of B
Approach I have taken:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date],LASTDATE(facts[date]), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
It produces correct results as long as there is at least one record for April.
When I use filter on segment_desc = 'B'
It produces result as I attached - so we see result in April equals 20, which is obviously not what I wanted. I would expect it to be 50.
Answer to the main question:
time intelligence functions like DATESINPERIOD require a proper calendar table, because they expect continuous dates, without gaps.
Answer to the follow-up question "why the measure shows value for January?"
It's a bit tricky. First, notice that LASTDATE in this filter context returns blank:
So, your DAX measure then becomes this:
last 3 value =
VAR DATES = DATESINPERIOD(facts[date], BLANK(), -3,MONTH)
RETURN CALCULATE([sum value], DATES)
Blank - 3 month does not make sense, so the way DAX resolves this: it replaces BLANK with the first (min) date in the table. In this case, it's 1/1/2021. Then it goes back 3 months from that date. As a result, the final measure is:
last 3 value =
CALCULATE([sum value], {2020-11-01, 2020-12-01, 2021-01-01 })
Since you have no data prior to 2021-01-01, the final result shows only January values.

Why does my measure with SumX return this result?

I have 2 tables:
**Partners**
PartnerID Name
1 AAAA
2 BBBB
3 CCCC
4 DDDD
**Sales**
PartnerID SaleAmount
1 15
2 20
3 30
4 40
1 15
I have a visual with PartnerID from my Partners table and this measure:
TotalSalesMeasure: Sumx(Partners, Sum(Sales[SaleAmount]))
**Resulting table visual with measure**
PartnerID TotalSalesMeasure
1 30
2 20
3 30
4 40
What's confusing me is how the results are derived. It's my understanding that:
-The partners table is filtered using the incoming context(PartnerID)
-Filtered partners table is iterated through and Sum(Sales[SaleAmount]) is called for each row
-After Iteration is done, it is summed
First row ex:
-Partners is filtered to 1 row based on the partnerID
-Since it's using row context and not filter context in the partners table, it sums the entire Sales[SaleAmount] column one time
-That should result in 15+20+30+40+15 = 120, but it shows 30
I was basing this on a video here at around the 49 min mark where he does a similar operation:
https://www.youtube.com/watch?v=1yWLhxYoq88
What's odd is if I do the same thing he does later by wrapping Sum with Calculate, I get the same result(aside from the totals field). In fact, my result seems to be what calculate would return(again, outside of the grand total)
I'm obviously confusing something, but I don't know what it is
Edit:
I think I know what's going on now. The external filter context is applied to the sales table before summing. I didn't realize that it did that as well
SumX(
Partners <---Affected by exterior context
Sum(Sales[SalesAmount]) <---Affected by exterior context + row context
)

Power Bi DAX: Divide Value and set it for each week

I have a target value of 20 for January but it is 20 for the month, i need to show this over each week. I have to divide the target by 4 if there are 4 weeks in a month and by 5 if there are 5 weeks in a month. It is as simple as that, i am using a line and clustered column chart to display my data, i need the target spread out into each week of the month. I also need another field to do this but im sure i can replicate your formula and make it applicable.
I have added a WeeksinMonth column that counts how many weeks are in a particular month e.g January has 5 weeks and February has 4 weeks. I need an IF statement that will divide the target and value by how many weeks in a month. e.g if month has 5 weeks then divide target and value by 5, if month has 4 weeks divide target and value by 4.
I have a calendar table with week values which i can used to put the divided target into and also the desired output i made in excel (See attached).
How will i go about this?
Desired Output
Calendar Table
enter code hereYou can create an extra column in your calendar table:
WeekMax =
var stOfMonth = Weeks[Start of Week].[Month]
var stOfYear = Weeks[Start of Week].[year]
return CALCULATE(max(Weeks[Weekinmonth]);FILTER(Weeks;Weeks[Start of Week].[Month] = stOfMonth && Weeks[Start of Week].[Year] = stOfYear))
Make sure you have a relation in your model between calendar and target date. Now you can use this number for that week/date to divide by.
You can now add an extra column in your target table:
WeeklyTarget = Targets[Target]/Related(Calendar[WeekMax])

Print Specific Rows and Columns in Pandas

I am having trouble printing certain values within my CSV.
I have a file that has 9 columns
Record_id Month Day Year Location_id Animal_id Sex Length Weight
and over 1000 rows.
I want to print Month , Day , and Year columns when the year is equivalent to 2002.
Because I have a lot of data I decided to only work with the first 5 rows where year is equal to 2002.
This is my code:
data.df.iloc[0:5, 1:4]
With this I can print the first 5 rows and the 3 columns I desire. However I can't figure out how to filter the year to be 2002
you can start by getting all the rows where year equal to 2002
with
filtered_data = df[df["Year"]==2002]
then you can apply your code to get only the first five rows and the three selected columns with
filtered_data.iloc[0:5, 1:4]

Using ArrayFormula for a running total causes a circular reference

See attached Google Sheet for example/sample of my issue. I am trying to use an ARRAYFORMULA() to keep a running total in column C, based on adding the previous row's value in column C with the current row's value in column B. Before I make it an ARRAYFORMULA, it works fine. In column F-H, you can see I tried the array version and get a "circular dependency" error.
The working formula:
=B3+C2 (this is in cell C3)
The non-working formula (with the desired ARRAYFORMULA):
=ArrayFormula(G3:G+H2:H) (this is in cell H3)
My question is, what adjustments can I make to my formula that will allow me to auto-fill this formula down the page as new rows are added (and not populating data for blanks).
Name Days Total Days
Jane 5 6
Tom 2 8
Billy 4 12
Sue 6 18
Sally 1 19
Sample document
=ARRAYFORMULA(IF(B2:B="", ,SUMIF(ROW(B2:B), "<="&ROW(B2:B), B2:B)+1))
demo spreadsheet
if this is what you need ( to always sum total in C column ),
then you don't need arrayformula you can use this simple cell formula (this is C2 formula):
=B2+(If(ISNUMBER(C1), C1, 0))
and spread it down the page, here is the sheet I captured
cheers, kres