Supplies are limited. Contact info@espc.tech right away to save your spot before the conference sells out.
Get your discountScore big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount
Hello, So I have a formula that counts the less that ones already in excel, but I can't seem to replicate in power bi, How would you go about doing this. I read about using the splicer but when I press the carrot all it says is dropdown and list? Am I missing something? Brand new to Power BI btw
There are multiple ways to go about this. The most important question I have is "how do you want this output to look?"
Then the follow up questions
The most versatile answer I have is to give you without more detailed information is code for a DAX measure that returns a count of items where the value is less than a currently selected value (selected either through placement in a table, or a slicer that selects only one item, etc):
CountLessThan = COUNTROWS(FILTER(ALL(Table), Table[Date] <= SELECTEDVALUE(Table[Date]) ) )
Or this if you're using a slicer to select the maximum date:
CountLessThan = COUNTROWS(FILTER(ALL(Table), Table[Date] <= MAX(Table[Date]) ) )
Yes a card is the end goal for this.
@bwhitt13864 wrote:Yes a card is the end goal for this.
I think you are confused. A slicer and a card are different things.
That is very well possible lol, I have it done in excel i'm just trying to show the data in a dashboard .
Can you take a screenshot and show us what you have in excel? We can help you replicate it in Power BI if we know what our outcome is supposed to look like.
If you want this number displayed in a card, how would you want a user to select a date to count all the previous entries from? Through a slicer selection? Clicking a row in a table? Clicking a data point in a graph?
Ahhh, there a multiple columns of dates and you're comparing by row! We all thought there was only one date column.
So you're trying to count all the black ones, or all the red ones? This will count the red ones:
CountOf5+YearGaps = COUNTROWS(FILTER(SUMMARIZE(Data, Data[Last Audit], Data[OldDate], "Days Between", DATEDIFF([OldDate],[Last Audit], DAY)), [Days Between]>= 5*365))
Since this is using the actual number of days in 5 years (5*365), you will get some different results. It looks like your excel formula rounded up to 5 years at 4.5, which may or may not be intended behavior.
If you have some sort of ID field on each row (like a company ID or audit number), I would add that as a field in the SUMMARIZE section, right after Data[Old Date]. Right now if two or more audits have the same Last Audit and OldDate, they are only counted once.
@Cmcmahan wrote:Ahhh, there a multiple columns of dates and you're comparing by row! We all thought there was only one date column.
So you're trying to count all the black ones, or all the red ones? This will count the red ones:
CountOf5+YearGaps = COUNTROWS(FILTER(SUMMARIZE(Data, Data[Last Audit], Data[OldDate], "Days Between", DATEDIFF([OldDate],[Last Audit], DAY)), [Days Between]>= 5*365))Since this is using the actual number of days in 5 years (5*365), you will get some different results. It looks like your excel formula rounded up to 5 years at 4.5, which may or may not be intended behavior.
If you have some sort of ID field on each row (like a company ID or audit number), I would add that as a field in the SUMMARIZE section, right after Data[Old Date]. Right now if two or more audits have the same Last Audit and OldDate, they are only counted once.
Thank you but I don't know where I would put that in this software. I'm sorry I can add the data but where can I put a function
Sure. I've created this .pbix file from your example data, showing off a few ways to calculate this value. I made up some names for your fields, since the screenshot you provided doesn't give one.
In the Fields pane to the right of the screen, you should see the table. Right click the table you want this Measure to be associated with (the association to a specific table doesn't actually DO anything, but it's useful in keeping measures organized with the table they calculate from), and select New Measure.
From there, you will get a text box at the top of the screen appearing. You would put this code into that text box and change the tables/fields in the code to have names that match the ones in your dataset. Once the measure is created, you can drag it into a card visual like any other field.
In the example file I linked, you can click any of the measure I created (they have a little calculator icon in the fields pane) and see the code used to create them across the top of the screen. I think I also misread your requirements (again) in my excitement about having two dates to compare. Looking at which dates are red/black, I've realized that you actually want to count dates that are 5 years or older than TODAY, instead of some other date. I've created the example file accordingly.
The easy way to figure out how long ago something was is to use the DATEDIFF function in DAX. However, it only gives whole numbers, and rounds up, so any rows that are 4.5+ years old round up to 5, which would be counted in the wrong category. You could use DATEDIFF and have it count the days instead of the years, and do the math of desired years * 365 days yourself, but that doesn't account for leap years, and looks messy.
Days Since = DATEDIFF(SELECTEDVALUE(AuditInfo[Founding Date]), TODAY(), DAY)
Years Since = DATEDIFF(SELECTEDVALUE(AuditInfo[Founding Date]), TODAY(), YEAR)
The clean way to do it is to use the YEARFRAC function in DAX. This gives you a decimal value for year difference, that doesn't automatically round up. This looks much better, and can be easily used to get an accurate answer to "is this date older than 5 years from now?".
Years Since (Frac) = YEARFRAC(SELECTEDVALUE(AuditInfo[Founding Date]), TODAY())
Now that we've determined the best way to get a clean result, we need to count the rows where this is measure is greater than or equal to 5. However, measures don't exist in a row on a table, they're calculated on the spot in the current context. You can read more about that here. However, there's a handy function called SUMMARIZE that can be used to create a temporary table that we CAN count the rows of.
This takes the table AuditInfo, groups all the rows by the same [Location ID], and adds a field called Years Since Founding Date that is populated by the measure [Years Since (Frac)]. It then filters this virtual table where the Years Since Founding Date is greater than or equal to 5.
CountOlderThan5Yrs = COUNTROWS( FILTER( SUMMARIZE(AuditInfo, AuditInfo[Location ID], "Years Since Founding Date", [Years Since (Frac)]), [Years Since Founding Date] >= 5.0 ) )
I added a [Location ID] to the table (it's just a number that's unqiue to each row), so that when I used SUMMARIZE, I could guarantee uniqueness of each row. I did this because if some locations had the same Founding Date, they would get grouped into one entry and only count once in the measure. You can see the results of this in the card to the bottom right. For your real data, you can either add an index, or use a pre-existing field that's unique to each row to group by.
I also set up conditional formatting on the Founding date, just like you did in Excel. You can right click the Founding Date field in the Values bucket of the Visualization pane to see how I set that up.
Hopefully that long winded explanation helps you solve your problem. If you have further questions, feel free to ask.
Slicers will automatically turn in to a slider if you drag an drop a date formatted field into it. It sounds like your data type isn't set to date. To fix this, go to your data, select the column that has the date values, and set the data type to "Date" in the modeling tab.
Is a slicer what you were looking for? Or are you wanting to do it automatically?