How to count frequency of data in dataset

Hi ,

 

Do we have any function in appian which can count frequency of data in dataset.

Test Dataset -

             Column 1 Column 2 Column 3

 Row 1 -     A           B             C

 Row 2-      A           B             D

 Row 3 -     C           B             E

 Row 4-      C           A             F

 Row 5-      C           G             H

 

Here i want to put one validation that combination of row 1 and row 2 should not be same . For that i need one frequency function which can return frequency of data in Column A & Column B. Here we have A & B coming in two rows which should not like that.

 

Kindly help me if anyone knows this.

I need to put one validation rule which should check frequency of

  Discussion posts and replies are publicly visible

  • I'm assuming you mean this is a grid that is presented to the user and not data that's coming from the database. While there's no out-of-the-box rule that will give you this, the following expression can serve as a starting point:

    = load(
      local!data: {
        { c1: "A", c2: "B", c3: "C" },
        { c1: "A", c2: "B", c3: "D" },
        { c1: "C", c2: "B", c3: "E" },
        { c1: "C", c2: "A", c3: "F" },
        { c1: "C", c2: "G", c3: "H" }
      },
      
      a!foreach(
        items: local!data,
        expression: with(
          difference(
            intersection(
              wherecontains(property(fv!item, "c1", null), property(local!data, "c1", null)),
              wherecontains(property(fv!item, "c2", null), property(local!data, "c2", null))
            ),
            fv!index
          )
        )
      )
    )
    

    This expression returns an array containing the rows that contain the same keys (in your case, c1 and c2), and an empty array if there are no matching rows at that index. It probably won't scale very well, so please keep an eye on performance for your specific data sets.

    If you're trying to check this for data that's already in the database, I would suggest you do this in the database server and not in Appian.

  • Thanks for the inputs.

    Basically we are having a full dataset which i am passing to another rule after applying some filters on UI. Then further back same data is getting append in main dataset and going to DB for CRUD operation. My intention is to do this validation on grid page itself by using same master data set or sub data set which we are populating on grid. I know this validation would be costly with DB hit so i am trying to save that approach only. So i want to put validation and mark it red if we select same combination in that grid.

  • Ok, so you can use the expression I provided earlier as a starting point
  • For this, You can apply the validation at field level

    like for column 1

    a!gridRowLayout(
    contents:{
    a!textField(
    label:"Column 1",
    value:ri!data[ri!index].c1,
    saveInto:ri!data[ri!index].c1,
    validations:{
    if(
    count(
    whereContains(
    ri!data[ri!index].c1,
    ri!data.c1
    )
    )>1,
    "Data is duplicate",
    {}
    )
    }
    )
    }
    )
  • 1. Let's name the master data you have as coreData having 3 columns (col1,col2,col3)
    2. Create an expression rule named filterData which accepts input of cdt type of coreData (NO MULTIPLE)
    Code for the rule...
    if(
    rule!apn_isEmpty(ri!input),
    null,
    if(
    ri!input.col1=ri!input.col2,
    null,
    ri!input
    )
    )
    3. To use this rule you can use a!foreach but you need to filter out null from the output
    Code for the same...
    filter(
    fn!isnull,
    a!foreach(expression:rule!filerData, input:ri!coreData)
    )

    Above rule will return coreData without rows that you don't expect and it will also NOT have null values.
    You can use above output as source for todatasubset function and can show it in grid.

    Hope this will be helpful!
  • Thanks a lot. Very simple & neat solution for this problem.
  • I applied this approach. But i got to know one knew issue in this approach. As per requirement. I need to match combination of two columns in one dataset . But when i am trying to put this condition its checking count correct but not at horizontal level its checking vertical level. I mean if i have data like

    B 1
    B 2
    C 3
    D 2
    B 3

    In this case its showing error for row 2 (B 2). because count of both is more than one irrespective of any row. But our requirement is to check this combination at row level and it should give use count of that.

    Can you help me in that. I am sharing code snipped which i written on basis of your approach.


    validations: {
    if(or(rule!APN_isBlank(ri!Data[ri!index].sgId),rule!APN_isBlank(ri!Data[ri!index].mtId)),{},if(
    and(
    count(
    whereContains(
    ri!Data[ri!index].sgId,
    local!scData.sgId
    )
    ) > 1,
    count(
    whereContains(
    ri!Data[ri!index].mtId,
    local!scData.mtId
    )
    ) > 1
    ),
    "Duplicate Combination ",
    {}
    ))
    }

  • 0
    A Score Level 1
    in reply to sauravk
    You have to apply this on both fields


    if(or(rule!APN_isBlank(ri!Data[ri!index].sgId),rule!APN_isBlank(ri!Data[ri!index].mtId)),{},
    count(
    whereContains(
    ri!Data[ri!index].sgId,
    local!scData.sgId
    )
    ) > 1,
    count(
    whereContains(
    ri!Data[ri!index].mtId,
    index(index(ri!Data,whereContains(
    ri!Data[ri!index].sgId,
    local!scData.sgId
    )),
    "mtId",
    {}
    )
    )
    )>1,
    "Duplicate Combination ",
    {}
    )