Splunk Search

Expression for custom lookup table values

bcanfield83
Engager

Hi All,

This may be a bit of a peculiar question, but I'm trying to figure out if there's a way to use a certain expression in a search query to pull a "maximum" value based upon a custom table (.csv import) that is pulled into the query via the "lookup" command.

The table has 4 possible "Attribute" values which range from "level-1-access" to "level-4-access". In the stats table, a given UserID may have activity that reflect 1 or more of these (thus, a maximum of 4 per UserID).

Below is a sample dataset. What I'm attempting to do is filter this data so that it's only showing the "maximum" (or, "highest") value for each UserID. The rows bolded in green is what I'd want to see, with everything else excluded; thus, there should only be 1 row per distinct UserID.

One possible thought that comes to mind is adding an numeric field to the .csv lookup, though still not 100% certain how to go about rendering the stats table to only include the highest value per UserID. 

Any help would be appreciated. Thanks! 

UserIDAttribute
jdoelevel-1-access
jdoelevel-3-access
jdoelevel-4-access
asmithlevel-1-access
asmithlevel-2-access
ejoneslevel-3-access
ejoneslevel-4-access
pthomaslevel-1-access
pthomaslevel-2-access
pthomaslevel-3-access
pthomaslevel-4-access
Labels (3)
0 Karma
1 Solution

dtburrows3
Builder

I think doing something like this would work.

 

 

 

<base_search>
    | lookup <lookup_name> UserID OUTPUT Attribute
    | eval
        attribute_regex=".*\-(\d+)\-.*",
        max_attribute=case(
            isnull(Attribute), null(),
            mvcount(Attribute)==1, max(tonumber(replace(Attribute, attribute_regex, "\1"))),
            mvcount(Attribute)>1, max(mvmap(Attribute, tonumber(replace(Attribute, attribute_regex, "\1"))))
            ),
        max_attribute_full=mvdedup(
            case(
                isnull(Attribute), null(),
                mvcount(Attribute)==1, if(tonumber(replace(Attribute, attribute_regex, "\1"))=='max_attribute', 'Attribute', null()),
                mvcount(Attribute)>1, mvmap(Attribute, if(tonumber(replace(Attribute, attribute_regex, "\1"))=='max_attribute', 'Attribute', null()))
                )
            )

 

 

 


You can see in the screenshot below I used simulated data to do what I think you are asking for.

dtburrows3_0-1703717237716.png

 

 

The regex used in the replace command can be adjusted to fit the pattern that is stored in the Attribute field value to just grab the number.

View solution in original post

bcanfield83
Engager

Thank you very much!!! 

0 Karma

dtburrows3
Builder

I think doing something like this would work.

 

 

 

<base_search>
    | lookup <lookup_name> UserID OUTPUT Attribute
    | eval
        attribute_regex=".*\-(\d+)\-.*",
        max_attribute=case(
            isnull(Attribute), null(),
            mvcount(Attribute)==1, max(tonumber(replace(Attribute, attribute_regex, "\1"))),
            mvcount(Attribute)>1, max(mvmap(Attribute, tonumber(replace(Attribute, attribute_regex, "\1"))))
            ),
        max_attribute_full=mvdedup(
            case(
                isnull(Attribute), null(),
                mvcount(Attribute)==1, if(tonumber(replace(Attribute, attribute_regex, "\1"))=='max_attribute', 'Attribute', null()),
                mvcount(Attribute)>1, mvmap(Attribute, if(tonumber(replace(Attribute, attribute_regex, "\1"))=='max_attribute', 'Attribute', null()))
                )
            )

 

 

 


You can see in the screenshot below I used simulated data to do what I think you are asking for.

dtburrows3_0-1703717237716.png

 

 

The regex used in the replace command can be adjusted to fit the pattern that is stored in the Attribute field value to just grab the number.

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Kick the Tires Before You Commit: A Hands-On Tour of the Splunk Observability Cloud ...

Evaluating an enterprise observability platform usually goes like this: fill out a form, get a free trial with ...

Deep insights, no barriers: Splunk Observability Cloud Free Edition

As software delivery cycles continue to accelerate, observability shouldn’t be a luxury — it should be a ...

Monitoring AI Agents with Splunk Observability Cloud

Let’s say I’m running a travel planning AI app in production. A user asks for three concise hotel options in ...