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
electrobrit
Post Patron
Post Patron

Count based on condition of 2 dates using OR operator

I have data which has customer data and if applicable, member data.
Member is determined by if they have a member # so I first counted how many total members by counting Members with member Total Members=# count(Members(Member#)
Then there is a need to find out of these members which ones are ACTIVE
-active is enrolled in the last 3 months OR a purchase within the last 18 months

I wasn’t sure how to do this so my process began as creating 2 conditional columns
Enrolled in last 3 months= IF(EDATE(‘Members’[Enrollment Date], 3)>TODAY(),1,0)
Purchase within 18 months = if(EDATE(‘Members’[Purchase Date], 18)>TODAY(), 1,0)
then I couldn’t figure out how to get that in it’s own measure with the operator OR.

(maybe there is another way besides using these too)

Active Member= Out of total members, how many are enrolled in last 3 months OR purchased within 18 months
Can someone help with this DAX?
thanks in advance

1 ACCEPTED SOLUTION

Hi @electrobrit , @Anonymous ,

Here is the pbix Active Members.

Here is the advanced editor, below. I created some test columns and late deleted them. However if you look closely you will see the code for the various items, like AddMonths. M is very particular on capitalization. You should pick it up quickly.  This forum is a good place for answers. Or google M language, and whatever code you are looking for.  Being able to read technical is really important.

 

DateTime.Date(DateTime.LocalNow())) is the syntax for Today()

 

There might be some filters on the table in the ver that I sent you. So you can turn them off just like in Excel at the top of the column.

The only measure I used was summing up the count. 

Active members = SUM('members'[New or Active])
 
If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

 

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\nat_c\OneDrive\Desktop\if emp purch.xlsx"), null, true),
    purchasemember_Table = Source{[Item="purchasemember",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(purchasemember_Table,{{"name", type text}, {"Member #", Int64.Type}, {"Enrolled on ", type date}, {"Last purchase", type date}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Member #", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type1", "Custom", each if [#"Enrolled on "] < #date(2019, 9, 22) then 1 else 0),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Custom.1", each if [Last purchase] < #date(2019, 9, 22) then 1 else 0),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column1", "Custom.2", each DateTime.Date(DateTime.LocalNow())),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.3", each Date.AddMonths(Date.From([Custom.2]), Number.From(-3
))),
    #"Added Conditional Column2" = Table.AddColumn(#"Added Custom1", "Custom.4", each if [#"Enrolled on "] >= [Custom.3] then 1 else 0),
    #"Added Custom2" = Table.AddColumn(#"Added Conditional Column2", "Custom.5", each Date.AddMonths(Date.From([Custom.2]), Number.From(-18))),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom2",{{"Custom.2", "Today"}, {"Custom.3", "90 days ago"}, {"Custom.5", "540 days ago"}}),
    #"Added Conditional Column3" = Table.AddColumn(#"Renamed Columns", "Active Purchase", each if [Last purchase] >= [540 days ago] then 1 else 0),
    #"Renamed Columns1" = Table.RenameColumns(#"Added Conditional Column3",{{"Custom.4", "Active new"}}),
    #"Added Conditional Column4" = Table.AddColumn(#"Renamed Columns1", "Custom.2", each if [Active new] = 1 then 1 else if [Active Purchase] = 1 then 1 else 0),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column4",{"Custom", "Custom.1"}),
    #"Renamed Columns2" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "New or Active"}}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns2",{{"Today", type date}, {"90 days ago", type date}, {"Active new", Int64.Type}, {"540 days ago", type date}, {"Active Purchase", Int64.Type}, {"New or Active", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type2",{{"name", Order.Ascending}}),
    #"Renamed Columns3" = Table.RenameColumns(#"Sorted Rows",{{"540 days ago", "18 months ago"}, {"90 days ago", "3 Months ago"}})
in
    #"Renamed Columns3"

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

11 REPLIES 11
electrobrit
Post Patron
Post Patron

DATA EXAMPLE 

   
nameMember #Enrolled on Last purchase enrolled eligiblepurchase eligible
john12341/1/201012/1/2018  1
lester23452/18/20183/2/2019  1
lucy34565/1/20194/3/2019  1
kyle45679/9/20191/2/2019 1 
bail34567/2/20101/1/2019 1 
shirly56788/23/20162/3/2018 1 
tom12324/13/20161/1/2019   
hank145676/28/201912/31/2017   
frank234569/1/20194/5/2019 1 
liza4567812/3/20161/1/2019  1
penny812326/28/20199/6/2019 1 
johnnull     
hank49492/3/20189/9/2019  1
loretta60139/9/2019  1 
jimmie34909/9/2019  1 


note: as of Sept 22, enrollment in last 3 months would be after 6/22/19 
a purchase within 18 months would be after 3/31/18. Active Member falls in one of these.

TOTAL Members with Member #15
enrolled within 3 months7
OR (if not enrolled, they made a purchase in last 18 months)
a purchase within 18 months5
  
ACTIVE MEMBERS out of this group12


I can do this in sql but can't figure it out in Power BI.
Select Count (membernumber) from [dbo].[memberTable]
where
enrollmentdate >= DATEADD(MONTH, -3, GETDATE()) or
lastpurchase >= DATEADD(MONTH, -18, GETDATE())

Thank you!

Hi @electrobrit ,

First pass.  How conversant are you with Power BI and Power Query? You said you could do this SQL.

Nathaniel

Active members.PNG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




still learning how to translate between the 2

Lucy and Bail have the same number





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




 

 

Hi @electrobrit ,

Here is my table, so as you can see, I added some columns. Although we could have combined these, I left them separate in the interest of understanding and debugging.  I did this in Power Query as it represents less load on your system and it can refresh against your data. So I added a column for today, and one for less three months as well as a conditional to give me 1 or 0. The same with 18 months ago. A final conditional column that gives me a 1 for each active member which I then summed up for the Dax measure. My dates are different, as I used the month function

 

Let me know if you have any questions, and I can send you my file if you wish. 

 

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

 

 

Active members 1.PNG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Ok thanks for the solution. 


I would think all that is a little easier, but I can't get =today() to work in power query. I have looked up different options and oddly having issues finding, which makes me think, the rest of the calcs are going to give me issues. Can you give me your dax used to get to final solution please?  I've been on this issue for a while, perhaps my mind is not working. 
thank you!

@electrobrit ,

I will send you the particulars in a couple of mins.

Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @electrobrit , @Anonymous ,

Here is the pbix Active Members.

Here is the advanced editor, below. I created some test columns and late deleted them. However if you look closely you will see the code for the various items, like AddMonths. M is very particular on capitalization. You should pick it up quickly.  This forum is a good place for answers. Or google M language, and whatever code you are looking for.  Being able to read technical is really important.

 

DateTime.Date(DateTime.LocalNow())) is the syntax for Today()

 

There might be some filters on the table in the ver that I sent you. So you can turn them off just like in Excel at the top of the column.

The only measure I used was summing up the count. 

Active members = SUM('members'[New or Active])
 
If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

 

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\nat_c\OneDrive\Desktop\if emp purch.xlsx"), null, true),
    purchasemember_Table = Source{[Item="purchasemember",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(purchasemember_Table,{{"name", type text}, {"Member #", Int64.Type}, {"Enrolled on ", type date}, {"Last purchase", type date}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Member #", type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Changed Type1", "Custom", each if [#"Enrolled on "] < #date(2019, 9, 22) then 1 else 0),
    #"Added Conditional Column1" = Table.AddColumn(#"Added Conditional Column", "Custom.1", each if [Last purchase] < #date(2019, 9, 22) then 1 else 0),
    #"Added Custom" = Table.AddColumn(#"Added Conditional Column1", "Custom.2", each DateTime.Date(DateTime.LocalNow())),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.3", each Date.AddMonths(Date.From([Custom.2]), Number.From(-3
))),
    #"Added Conditional Column2" = Table.AddColumn(#"Added Custom1", "Custom.4", each if [#"Enrolled on "] >= [Custom.3] then 1 else 0),
    #"Added Custom2" = Table.AddColumn(#"Added Conditional Column2", "Custom.5", each Date.AddMonths(Date.From([Custom.2]), Number.From(-18))),
    #"Renamed Columns" = Table.RenameColumns(#"Added Custom2",{{"Custom.2", "Today"}, {"Custom.3", "90 days ago"}, {"Custom.5", "540 days ago"}}),
    #"Added Conditional Column3" = Table.AddColumn(#"Renamed Columns", "Active Purchase", each if [Last purchase] >= [540 days ago] then 1 else 0),
    #"Renamed Columns1" = Table.RenameColumns(#"Added Conditional Column3",{{"Custom.4", "Active new"}}),
    #"Added Conditional Column4" = Table.AddColumn(#"Renamed Columns1", "Custom.2", each if [Active new] = 1 then 1 else if [Active Purchase] = 1 then 1 else 0),
    #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column4",{"Custom", "Custom.1"}),
    #"Renamed Columns2" = Table.RenameColumns(#"Removed Columns",{{"Custom.2", "New or Active"}}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns2",{{"Today", type date}, {"90 days ago", type date}, {"Active new", Int64.Type}, {"540 days ago", type date}, {"Active Purchase", Int64.Type}, {"New or Active", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type2",{{"name", Order.Ascending}}),
    #"Renamed Columns3" = Table.RenameColumns(#"Sorted Rows",{{"540 days ago", "18 months ago"}, {"90 days ago", "3 Months ago"}})
in
    #"Renamed Columns3"

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




thank you, I did get "today". If you got it, would like to see what you did. i am new to dax

Hi @electrobrit , 

So I showed the data on a matrix, and sorted the enrolled on date from the top. The line shows above are new enrollees, and below are the active purchasers.  I count 13.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

 

Active members 4.PNG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hi,

can you share a sample of your data?

Thanks.

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.