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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
Anonymous
Not applicable

calculate and If function in measure

Hi,

 

I have data in following format. 

 

I need to calculate average balances based on total days in a month except that calculation is different for each source. 

 

sourcedatebalanceweekday
a05-Nov100Monday
b05-Nov250Monday
a04-Nov150Sunday
b04-Nov200Sunday
a03-Nov120Saturday
b03-Nov200saturday
a02-Nov130Friday
b02-Nov200Friday
a01-Nov160Thursday
b01-Nov230Thursday
a31-Oct140Wednesday
b31-Oct220Wednesday

 

If source = a, sum(balance)/total days in the month

If source = b, sum(balance)/total woking days in a month

 

I have created a date table that gives me working days (1) and non working days(0).

 

I tried using calclulate and if function together but that doesnt seem to be working. 

 

Each source has a subdivision. I need to show my output in the following manner (Numbers are random) for a particular month selected on a slicer. 

 

 average balance
a.1100
a.2200
a.3300
b.1150
b.2250
b.3300

 

Can someone help. Thanks in Advance!

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thinking using DISTINCTCOUNT instead of COUNTROWS will work ('Calc and IF' is the name of the table):

 

CountRows Weekday = 
CALCULATE(
    DISTINCTCOUNT('Calc and IF'[date]),
    'Calc and IF'[Isweekend]=0)

CountRows Weekend =
CALCULATE(
DISTINCTCOUNT('Calc and IF'[date]),
'Calc and IF'[Isweekend]=1)

It will give you the disctinct count based on the current filter context.  So if you just have Source in the current filter context, will get 1 for each source A and B, since each source has a weekday and a weekend record

Disct Count just source.png

 

Samething happens when you add in Client ID, except that aC4 and bD4 only have a count (1) for Weekeday and blank for Weekend since the filter context is saying let's get the distinct count of source = a, Client = C4, and IsWeekend=  1, which doesnt exist:

disct count source and client.png

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Let's see if this can get you in the right direction.  Going off the data in your first table, created a calculated column to Flag Weekends ( 1 = weekend, 0 = weekday):

Calc Column.png

 

Then create a measure for the balance total.  I always use explicit measures, never implicit (i.e. just checking the box next to the balance):

 Balance.png

 

Now, we need to count the Working Days and Weekend Days.  The following is basic and will need some further refinement when used with more data, but the core of the idea is there:

 Working Weekend Days.png

 

Total Days is just the sum of those above two measures:

Total Work Days.png

 

Now need to know when to use the Total Working Days Measure as the denominator, or the Total Days measure.  Since we only have two choices (A or B ) nested ifs would work, but they are clunky and if you start getting more choices even more so.  I opted to use SWITCH.

Switch.png

 

Then from there the final piece is to use the DIVIDE function:

Divide.png

 

Final Table ( the names of the measures should be something smaller, but left them more descriptive) and wouldn't put them all on a table

Final Table.png

 

I think that should get you in the right direction.  With the two COUNTROWS measures, there will be some more considerations when you have more data ( i.e. only count number of days up to a specific point in a specific month) but those are pretty easy to add in.

Anonymous
Not applicable

Hi @Anonymous,

 

Thanks this makes sense, but i have something more to add to. Each source has data from differernt clients like thi:

 

source ClientIDdatebalanceweekdayIsweekend
aC105-Nov10Monday0
aC205-Nov20Monday0
aC305-Nov30Monday0
aC405-Nov40Monday0
bD105-Nov50Monday0
bD205-Nov60Monday0
bD305-Nov70Monday0
bD405-Nov80Monday0
aC104-Nov20Sunday1
aC204-Nov30Sunday1
aC304-Nov40Sunday1
bD104-Nov60Sunday1
bD204-Nov70Sunday1
bD304-Nov80Sunday1

 

So, for calculating weekends and weekdays it is counting each client for count rows function. For example:

 

For 5th Nov: first 8 rows should be counted as 1 for each source, however it is counting as 4 each. Each source has differnt rows everyday due to different number of clients. How to avoid this error?

Hi @Anonymous,

 

Have you solved your problem?

 

If you have solved, please always accept the replies making sense as solution to your question so that people who may have the same question can get the solution directly.

 

If you still need help, please share your desired output so that we could help further on it.

 

Best  Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Thinking using DISTINCTCOUNT instead of COUNTROWS will work ('Calc and IF' is the name of the table):

 

CountRows Weekday = 
CALCULATE(
    DISTINCTCOUNT('Calc and IF'[date]),
    'Calc and IF'[Isweekend]=0)

CountRows Weekend =
CALCULATE(
DISTINCTCOUNT('Calc and IF'[date]),
'Calc and IF'[Isweekend]=1)

It will give you the disctinct count based on the current filter context.  So if you just have Source in the current filter context, will get 1 for each source A and B, since each source has a weekday and a weekend record

Disct Count just source.png

 

Samething happens when you add in Client ID, except that aC4 and bD4 only have a count (1) for Weekeday and blank for Weekend since the filter context is saying let's get the distinct count of source = a, Client = C4, and IsWeekend=  1, which doesnt exist:

disct count source and client.png

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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