Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
nandic
Memorable Member
Memorable Member

Calculating expected income for the selected year

Hi, below is my sample data:

     If selected Year Month = February 2020 month columns below do not exist in model, they are shown to display desired logic
     201920192019202020202020202020202020202020202020202020202020202120212021 
Contract IDLength of Contract (Months)Contract Start DateContract End DateContract Monthly Value1.10.20191.11.20191.12.20191.1.20201.2.20201.3.20201.4.20201.5.20201.6.20201.7.20201.8.20201.9.20201.10.20201.11.20201.12.20201.1.20211.2.20211.3.2021Expected Total Amount
7382020-01-012023-03-01160000160160160160160160160160160160160160000   1.920,00
1132020-01-012020-04-01200000200200200000000000000      600,00
26362020-01-012023-01-01350000350350350350350350350350350350350350000   4.200,00
30362020-01-012023-01-01390000390390390390390390390390390390390390000   4.680,00
8122020-02-012021-02-011700000170170170170170170170170170170170000   1.870,00
12362020-02-012023-02-012100000210210210210210210210210210210210000   2.310,00
29232020-02-012022-01-013800000380380380380380380380380380380380000   4.180,00
19242020-02-242022-02-2428000000280280280280280280280280280280000   2.800,00
27242020-02-242022-02-2436000000360360360360360360360360360360000   3.600,00
2822020-02-272020-04-273700000037037000000000000      740,00
1222020-03-012022-01-0110000000100100100100100100100100100100000   1.000,00
21122020-03-012021-03-0130000000300300300300300300300300300300000   3.000,00
1732020-03-022020-06-02260000000260260260000000000      780,00
1532020-03-092020-06-09240000000240240240000000000      720,00



My goal is to calculate expected yearly income based on year of selected month. I am currently using disconnected date table as based on selected month i need to select all months in year (and later need to display charts with cumulative amounts per month).

Here is the logic: get year of selected month and only do calculations for months in year of selected month.
Example 1: if contract starts on February 2020 and ends in February 2021, calculate only for months in 2020.
Example 2: if contract starts on February 2020 and ends in June 2020, calculate for all months as they are all in 2020.
Example 3: compare start of month to the first of month. If contract start date is on March 1st 2020 then first month to have value is March 2020. If contract start is March 2nd 2020 then start from April 2020.

Final step is to sum all these values (last column on data above "Expected Total Amount").

Notes:
- first five column (heading in green color) exist and they are in one table.
Month columns are used to describe the logic, but i do not have that design so this is not pure If Then Else statement. So these columns should be created virtually or to use some kind of disconnected table.
I used generate function to generate months in measure variable, but something is not working properly on the final output.

Thank you in advance!

Regards,
Nemanja


1 ACCEPTED SOLUTION
3 REPLIES 3
v-yangliu-msft
Community Support
Community Support

Hi  @nandic  ,

I convert simulation data into test data according to unpivot columns

v-yangliu-msft_0-1611120656028.jpeg

Form a new table 2:

v-yangliu-msft_1-1611120656031.png

Here are the steps you can follow:

1. Create a calculated column in the table

Column =
var _startdate=
IF(DAY([Contract Start Date])=1,[Contract Start Date],DATE(YEAR([Contract Start Date]),MONTH([Contract Start Date])+1,1))
var _enddate=
DATE(YEAR([Contract Start Date]),12,31)
return
CALCULATE(SUM('Table (2)'[Value]),FILTER('Table (2)',[Date]>=_startdate&&[Date]<=_enddate&&[Contract ID]=EARLIER('Table'[Contract ID])))

2. Result.

v-yangliu-msft_2-1611120656033.jpeg

You can downloaded PBIX file from here.

 

If my answer is not what you need, can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

Hi @amitchandak ,
Thank you for provided links, second example is exactly what i needed!
Summarize part of your function works exactly as i wanted (i checked results), it generates row for each month that is in range between start and end date. And i just added one more column which returns value to be summed.
But when i add part of function to sum these values: calculate(sumx(.....)), instead of summing values it only returns value of one row (like it calculates min/max and not sum).
I tested same logic in your file and it works great!

Example in your file:
For ID = 1, there are 14 days and value for each day is 200, so sumx is 14*200=2800.
But in my report instead of 2800 it returns 200. Do you have any idea what might cause this?

Capture amit.PNG


On the end i found solution that works which has much more code than your solution (it has generate function, naturalinnerjoin, filters).
Your solution is much more friendly, just to find out why these values are not summed as expected.
In my report there are 2 tables and structure is same as in your file:
 1) data table where i have : id, start date, end date, value
 2) date table: date which is used for filtering

Regards,
Nemanja Andic

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.