# Filters in custom functions?

**URL:** <https://community.spicecrm.io/t/filters-in-custom-functions/48>\
**Category:** KReporter\
**Created:** [January 2, 2017, 4:10pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48 "2017-01-02T16:10:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralph](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@ralph](https://community.spicecrm.io/u/ralph)\
**Post date:** [January 2, 2017, 4:10pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/1 "2017-01-02T16:10:32Z")

</div>

Is it possible to use the fieldname reference created in a filters (select section) in a custom function of a field (manipulate section)?

---

<div class="post-metadata">

**Author:** ![maretval](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@maretval](https://community.spicecrm.io/u/maretval)\
**Post date:** [January 2, 2017, 5:30pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/2 "2017-01-02T17:30:50Z")

</div>

Ralph, no, reference in select section is used for unions (select over multiple modules). There is for sure an intention behind your question. What would you like to do?

---

<div class="post-metadata">

**Author:** ![ralph](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@ralph](https://community.spicecrm.io/u/ralph)\
**Post date:** [January 2, 2017, 5:53pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/3 "2017-01-02T17:53:20Z")

</div>

Hi,  
In a report I have a filter by a date field to select a variable period (year, month, quarter … or any two dates).

Then I need the start and end dates of the period selected in the filter to make a calculation through a custom function in one of the columns of the report (for example to determine the number of days that the period indicated in the filter includes).

As I understand it, I could use the variables {p1} and {p2} to refer to the values used in the filter, but I can not get it to work and I do not know if I’m interpreting it badly.

Specifically, I’m trying to use the filter values in a simple custom function … DATEDIFF ({p1}, {p2})

I do not see how to use the parameters passed in the filters ({p1} and {p2} if I’m not mistaken)

---

<div class="post-metadata">

**Author:** ![maretval](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@maretval](https://community.spicecrm.io/u/maretval)\
**Post date:** [January 3, 2017, 8:37am UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/4 "2017-01-03T08:37:13Z")

</div>

ok Ralph, if I understood you correctly following example should help you.  
My report lists opportunities name, amount, creation date, closed date and the number of days between creation and closed date. The selection only contains opportunities created in 2016.

 ![](https://canada1.discourse-cdn.com/flex030/uploads/spicecrm/original/1X/cee004408732362bb90b296738398f5e54528ddc.png)  
Now let me explain the content of customFunction:  
I dragged the field Date Created from Opportunities. I renamed it to DateDiff so that the column title has the same meaning as its value. Then I entered the SQL customFunction.  
{t} represents the name of the table from where the field is from.  
{f} represents the name of the field that you dragged.  
{t}.{f} is therefore the date\_entered field (creation date). That’s the one I dragged.  
{t}.date\_closed is the second field in the same table. The one we need to make calculation.  
Packed in the SQL CustomFunction we get DATEDIFF({t}.date\_closed,{t}.{f})

---

<div class="post-metadata">

**Author:** ![ralph](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@ralph](https://community.spicecrm.io/u/ralph)\
**Post date:** [January 3, 2017, 6:37pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/5 "2017-01-03T18:37:04Z")

</div>

Hi Maretval,  
Thanks for your answers.  
I will try to explain my needs with the example you give me:

- In the example that you mention you tries making a calculation with datediff, starting from a date that appears in a column returned in the own report. In the case of the example you want to calculate the date from another that you have (creation\_date) then on custom function in the column creation\_date you apply the formula Datediff ({t}. {F}, {t} .date\_closed) (or vice versa) and you get the result.

In the case I try to solve (putting an example with the same data as your example) would be like asking (and show it in the same report). How many opportunities win and lost of each client were there before the report date (2016-01-01)? For example, to show a comparative of opportunities of each client.

I’m try to use {p1} and {p2} variables in a custom funcion. My report includes all the oportunities between two dates {p1} {p2} (filter - selection tab), and I need to know the days between the first day of the period {p1} and the opportunity date\_entered field.

I try the custom function datediff({p1},{t}.{f}) in the date\_entered column, but don’t works.

Thanks

---

<div class="post-metadata">

**Author:** ![maretval](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@maretval](https://community.spicecrm.io/u/maretval)\
**Post date:** [January 3, 2017, 10:27pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/6 "2017-01-03T22:27:54Z")

</div>

Ralph, {p1} and {p2} are reserved names corresponding to Equals From / To when you define the selection operators in a custom kreporter field. They are not available in manipulate tab.  
You would like the Equals/From value to be used as a constant in manipulate tab, it is not possible.

Regarding the count of Won & Lost Opportunities per Account, you can use SUM combined with IF in custom Function.

 ![](https://canada1.discourse-cdn.com/flex030/uploads/spicecrm/original/1X/8681f8dfd804e7b53348b5a5de81d5ad113ac9ea.png)  
I grouped the report by Account ID and by sales stage.  
I dragged opportunities sales stage 3 times: 1 to apply group by, 1 to count when value is Closed Won, 1 to count when value is Closed Lost.  
SUM(IF({t}.{f} = ‘Closed Won’,1,0))

---

<div class="post-metadata">

**Author:** ![caiocarvalho](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@caiocarvalho](https://community.spicecrm.io/u/caiocarvalho)\
**Post date:** [April 24, 2018, 6:59pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/7 "2018-04-24T18:59:19Z")

</div>

Hello, Good afternoon!

can you give me an exemplo how I use {p1} and {p2} in custom formula?

best regard,

Caio Carvalho

---

<div class="post-metadata">

**Author:** ![maretval](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@maretval](https://community.spicecrm.io/u/maretval)\
**Post date:** [April 25, 2018, 9:48pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/8 "2018-04-25T21:48:07Z")

</div>

{p1} and {p2} can’t be used in custom formula.  
They can be used for custom kreporter fields.  
check documentation [https://spicecrm.gitbooks.io/kreporter-4-x/content/advanced/kreporter-field.html](https://spicecrm.gitbooks.io/kreporter-4-x/content/advanced/kreporter-field.html)

---

<div class="post-metadata">

**Author:** ![caiocarvalho](https://avatars.discourse-cdn.com/v4/letter/c/f17d59/32.png) [@caiocarvalho](https://community.spicecrm.io/u/caiocarvalho)\
**Post date:** [April 26, 2018, 3:11am UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/9 "2018-04-26T03:11:57Z")

</div>

I will check. thanks so much maretval…

---

<div class="post-metadata">

**Author:** ![rsp](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@rsp](https://community.spicecrm.io/u/rsp)\
**Post date:** [December 8, 2023, 5:20pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/10 "2023-12-08T17:20:40Z")

</div>

What is the function to filter date to display records of next month?

For example, I have date filed in the module and I want to display only records from the next month.

---

<div class="post-metadata">

**Author:** ![maretval](https://avatars.discourse-cdn.com/v4/letter/m/b38774/32.png) [@maretval](https://community.spicecrm.io/u/maretval)\
**Post date:** [December 10, 2023, 2:27pm UTC](https://community.spicecrm.io/t/filters-in-custom-functions/48/11 "2023-12-10T14:27:00Z")

</div>

The filter operator in the next n month shall do

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/spicecrm/original/1X/e06d574ab0d7d20464b4f95afd57646a797530a8.png)
