How to formulate on specific dates - if-statement

Lets say I have 5 invoices each having a total of 100
4 of the invoices are in the month of Jan and 1 in the month of Feb
I would like to sum ONLY the 4 invoices of Jan thereby giving me a total of 400
I would expect to write a formula such as if invoice.date in period 01.01.2015 to 31.01.2015 then sum invoice.total
however when I try this, the result is 500 as it's totalling all invoices and note only the 4 in the period.

Related

Dax Query- To Calculate MAXID and Sum of Amount

I have below sample data:
IncidentID TransactionID Recordeddate Name Amount
10 1 13/01/2023 Recovery 1000
10 2 13/01/2023 Reserve 2000
10 3 13/01/2023 Reserve 3000
10 4 14/01/2023 Reserve 4000
11 5 14/01/2023 Recovery 3000
11 6 14/01/2023 Payment 4000
12 7 14/01/2023 Reserve 5000
13 8 14/01/2023 Reserve 4000
13 9 14/01/2023 Payment 2000
I need to calculate sum of amount for each incident id, for each date only Max Transaction ID and Name should be Reserve only.
I tried doing it be below expression:
Table2 = summarize(Table,Table[IncidentID],Table[RecordedDate],Table[Name],"MAXID",
calculate(max(TransactionID),AllExcept(IncidentID,RecordDate,Name)))
this table is giving me required filtered table, but i am not able to add amount column.
If i add amount column grouping is not working, and i need to get amount included so that i can calculate sum of amount.
Expected- Not able to add amount column in this, as adding amount it is not giving required results.

While dividing two measures in DAX, How can I get a SUM of the measure to display correctly? The divide function doesn't work for me

I have a DAX formula that i have used:
Price = DIVIDE([Sales Amount], [Net Sales Quantity])
My issue is that the SUM of Price is dividing the totals of Sales Amount and Net Sales Quantity instead of displaying the SUM of Price correctly.
Here's an example of what i see in Power BI:
Product Code
Sales Amount
Net Sales Quantity
Price
AIU
70
10
7
JID
36
6
6
DII
10
5
2
TOTAL
116
21
5.5
As you can see above, the total for Price is not adding up column, instead its dividing the totals for Sales Amount and Net Sales Quantity which is 116/21 = 5.5
The actual result that I need is:
Product Code
Sales Amount
Net Sales Quantity
Price
AIU
70
10
7
JID
36
6
6
DII
10
5
2
TOTAL
116
21
15
As you can see, its simply adding up the Price and SUM for the price is correct.
I'm new to power bi, i've looked at documentation and have not been able to solve this. Any help is appreciated!
You can try with Column instead of Measure

Power Pivot - calculating distinctcount per week (rather than per day)

I am having problems with a distinctcount calculated by week. I have the pivot table below. I want to calculate the distinct number of vendors that have sold more than $2400 per week.
I have the following data table "sales" (only the first rows, but it has several vendors and other weeks as well):
sales day sales week vendor ID Total Sales
02.11.2020 45 vendor 1 405
03.11.2020 45 vendor 1 464
04.11.2020 45 vendor 1 466
05.11.2020 45 vendor 1 358
06.11.2020 45 vendor 1 420
07.11.2020 45 vendor 1 343
I have tried to calculate it as such:
= [vendor] =distinctcount('Sales'[vendor ID])
= [Total_sales] = sum('Sales'[Total Sales])
= [# vendors - 2400] =calculate([vendor],filter('Sales',[Total_sales]>2400))
I know that this calculation considers the sales per day, not per week. so, if instead of using $2400 I used $300, for instance, then both vendors would be marked, since in at least one day, the sales of both are higher than $300. But I only want to consider the sales in a weekly basis.
What I expect (check pivot table below): Vendor 2 would be marked (sales = 2456), but not vendor 1 (sales = 1341), i.e., total number of vendors = 1. However, none of the vendors are being counted, since no daily sales are higher then $2400
Row Labels # Vendors (distinct) total sales
Store A 3797
week 45 3797
Vendor 1 1341
02.11.2020 348
04.11.2020 202
05.11.2020 335
06.11.2020 308
07.11.2020 148
Vendor 2 2456
02.11.2020 405
03.11.2020 464
04.11.2020 466
05.11.2020 358
06.11.2020 420
07.11.2020 343
I also tried to create a column of sales in which I removed the day filter, like this:
=calculate([total_sales],ALL('sales'[sales day]))
and then recalculated the [# vendors - 2400], but it still gets me the same result as above.
The question is: how do I get to consider the total sales value per week (and not per day) for the distinctcount. Thank you for the help!
Do you have a Date calendar in your file? if no try to make one, then have a relationship from date to sales day (assuming this has your dates). That way you should be able to summarize by any date grouping eg, Month, Day, Week, Quarter etc...Or you can try parsing the other date field and add new columns to your table = weeknum(Tablename[sales day])

Power BI - Calculate Percentage Measure based on Top N Selection

I have a table like this.
Company Amount Year
A 200 2016
B 300 2016
C 400 2016
A 500 2017
B 600 2017
C 700 2017
A 100 2016
B 400 2016
C 100 2016
A 600 2017
B 133 2017
C 50 2017
I am looking for a measure calculate the Percentage of amount that top 2 companies(based on amount) contributes to that particular year's total amount. This needs to be dynamic based on the values of Year slicer. (For Example if 2 years are selected, then the top 2 companies needs to be based on the total amount that the company has spent on those 2 years).
How about this as a measure?
PercentTop2 =
DIVIDE(
SUMX(
TOPN(2,
SUMMARIZECOLUMNS(Companies[Company],
"Amount", SUM(Companies[Amount])),
[Amount]),
[Amount]),
SUMX(ALLSELECTED(Companies), Companies[Amount]))
The TOPN(2,[...]) finds the top 2 rows of the summarized table. Then you divide the sum of those two rows by the sum of all the selected rows.

Convert Daily Returns to Monthly Returns in Stata

I am using Stata and I have 6 years of daily returns for stocks that individuals hold in their portfolios. I would like to aggregate the daily returns to monthly portfolio returns. In some instances, the individual may hold more than one stock in the portfolio. I am struggling with writing the code to do this.
For a visual, my data looks like this:
I would like the results to look like this:
Where individual 2's portfolio return for the month of December 1996 is calculated as: 0.3 * 0.0031 + 0.7 * 0.0076 = 0.00625.
I have tried the collapse command such as
collapse Return, by (ID Year Month)
but this does not provide the same return that I calculated out in Excel.
I am able to make a weighted portfolio return for all the days using
bysort ID year month: egen wt_return = stock_weight * monthly_return
But this gives me daily returns. My trouble is then aggregating them into one return for the corresponding month.
As for the specifics, I would like to calculate the monthly portfolio return as the product of 1 + the weighted daily returns. As a last resort, the mean return for the month could work.
You don't show monthly portfolio return for person 2 in 1991. Your initial example data doesn't show stock weights but the desired example
data does. Your variable Monthly Return is not reproducible. You should take time to verify your question is clear when posting.
It's supposed be clear to the public who will read it, not only to you.
I didn't bother checking if your computations are correct but below is what I
understand you want. The procedure is simply to compute a weighted return and then
add them up by person year month groups. (I assume the stock weights apply to stocks on a daily basis, which is what your example data implies.)
clear all
set more off
input ///
perid year month day str3 stockid return stockw
1 1991 1 1 "ABC" .01 1
1 1991 1 2 "ABC" .02 1
1 1991 1 3 "ABC" -.01 1
1 1991 1 31 "ABC" .004 1
1 1996 12 31 "ABC" .002 1
2 1991 1 1 "ABC" .01 .3
2 1991 1 2 "ABC" .02 .3
2 1996 12 31 "ABC" .004 .3
2 1991 1 1 "XYZ" .001 .7
2 1991 1 2 "XYZ" .004 .7
2 1996 12 31 "XYZ" .021 .7
end
* create weighted return
gen returnw = return * stockw
sort perid year month day
list, sepby(perid year month day)
* sum weighted returns by person, year, month
collapse (sum) returnw, by (perid year month)
list, sepby(perid)
If you want collapse to sum, then you must indicate it with the (sum) (although I'm not clear if this is what you want). By default, it computes the mean. Read help collapse thouroughly.