SOLVED

mv-expand - I cannot make it work!!

%3CLINGO-SUB%20id%3D%22lingo-sub-3115872%22%20slang%3D%22en-US%22%3Emv-expand%20-%20I%20cannot%20make%20it%20work!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3115872%22%20slang%3D%22en-US%22%3E%3CP%3ECan%20anyone%20spare%20anytime%20to%20give%20me%20a%20basic%20example%20of%20how%20to%20use%20mv-expand%20please%2C%20so%20that%20I%20can%20then%20expand%20on%20it!%20(See%20what%20I%20did%20there%26nbsp%3B%3CIMG%20class%3D%22lia-deferred-image%20lia-image-emoji%22%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fhtml%2F%408341BD79091AF36AA2A09063B554B5CD%2Fimages%2Femoticons%2Fsmile_40x40.gif%22%20alt%3D%22%3Asmile%3A%22%20title%3D%22%3Asmile%3A%22%20%2F%3E)%26nbsp%3B%20I%20just%20don't%20get%20it.%26nbsp%3B%20I%20understand%20that%20it%20can%20be%20used%20to%20extract%20a%20value%20from%20an%20array%2C%20but%20in%20my%20fiddling%20it's%20not%20happening.%26nbsp%3B%20I%20have%20looked%20at%20the%20docs%20but%20the%20examples%20just%20don't%20relate%5Cclick%20with%20me.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20been%20enjoying%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F117380%22%20target%3D%22_blank%22%3E%40TeachJing%3C%2FA%3E%26nbsp%3BYoutube%20lessons%20-%20but%20I%20haven't%20found%20one%20that%20covers%20this%20command.%3C%2FP%3E%3CP%3EI'm%20just%20looking%20for%20the%20most%20minimal%20lines%20so%20I%20can%20build%20from%20it.%3C%2FP%3E%3CP%3EMany%20thanks%20(Soz%20for%20the%20stupid%20question!)%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3116742%22%20slang%3D%22en-US%22%3ERe%3A%20mv-expand%20-%20I%20cannot%20make%20it%20work!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3116742%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1287382%22%20target%3D%22_blank%22%3E%40CodnChips%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20get%20some%20code%20examples%20from%20the%20Sentinel%20Github%2C%20via%20the%20search%20box%2C%20then%20filter%20on%20%3CSTRONG%3EYAML%3C%2FSTRONG%3E%20files%20(to%20reduce%20some%20noise%2C%20the%20%3CSTRONG%3Ejson%3C%2FSTRONG%3E%20files%20can%20also%20be%20useful...but%20start%20simple!).%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Clive_Watson_0-1643981702869.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F345292i0A7367561D095387%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Clive_Watson_0-1643981702869.png%22%20alt%3D%22Clive_Watson_0-1643981702869.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EYou%20should%20end%20up%20with%20something%20like%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Fsearch%3Fl%3DYAML%26amp%3Bq%3Dmv-expand%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FAzure%2FAzure-Sentinel%2Fsearch%3Fl%3DYAML%26amp%3Bq%3Dmv-expand%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnother%20example%20of%20its%20use%20is%20where%20you%20have%20%5B0%5D%20and%20%5B1%5D%20type%20entries%20(often%20these%20are%2010%20or%20more).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Clive_Watson_1-1643981979440.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F345295iA4559AFA62A5DB81%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Clive_Watson_1-1643981979440.png%22%20alt%3D%22Clive_Watson_1-1643981979440.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BYou%20can%20use%20mv-expand%20to%20produce%20the%20data%20without%20the%20%5Bnumbers%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Clive_Watson_2-1643982084028.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F345296iFD68A78B82F69672%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Clive_Watson_2-1643982084028.png%22%20alt%3D%22Clive_Watson_2-1643982084028.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20can%20play%20with%20this%20example%20in%20the%20Logs%20Demo%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%23%404bd2cd73-7c32-48aa-8a02-646c8bc0d343%2Fblade%2FMicrosoft_Azure_Monitoring_Logs%2FDemoLogsBlade%2FresourceId%2F%252FDemo%2Fsource%2FLogsBlade.AnalyticsShareLinkToQuery%2Fq%2FH4sIAAAAAAAAAwvOTM%25252FLzPPJTy%25252FmqlEoKMrPSk0uUXAsLclIzSvJTE4syczPc0ktSczMAcnnlummVhQk5qUolOSnVOYl5mYma2BVqwlUoFBSWZCan6ZRXFKUmZeuCdSek5mbWaJgCADVP6g0cwAAAA%25253D%25253D%2Ftimespan%2FP1D%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EGo%20to%20Log%20Analytics%20and%20run%20query%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3116951%22%20slang%3D%22en-US%22%3ERe%3A%20mv-expand%20-%20I%20cannot%20make%20it%20work!!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3116951%22%20slang%3D%22en-US%22%3EHey%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1210469%22%20target%3D%22_blank%22%3E%40Clive_Watson%3C%2FA%3E%20thanks%20very%20much%20for%20your%20response.%20This%20is%20so%20helpful.%20I%20will%20have%20a%20look%20on%20the%20Github%20link%20and%20see%20what%20I%20can%20find%20as%20next%20step%20as%20Gary%20eluded%20to%20is%20connecting%20with%20a%20Join.%3CBR%20%2F%3EMany%20thanks%3C%2FLINGO-BODY%3E
Contributor

Can anyone spare anytime to give me a basic example of how to use mv-expand please, so that I can then expand on it! (See what I did there :smile:)  I just don't get it.  I understand that it can be used to extract a value from an array, but in my fiddling it's not happening.  I have looked at the docs but the examples just don't relate\click with me.

 

I've been enjoying @TeachJing Youtube lessons - but I haven't found one that covers this command.

I'm just looking for the most minimal lines so I can build from it.

Many thanks (Soz for the stupid question!)

16 Replies
best response confirmed by CodnChips (Contributor)
Solution

@CodnChips I tend to use it when trying to get the related alerts from an incident.   If you look at a row in the SecurityIncident table, you will see the AlertIds field is listed like: 

["695ef2b2-ceb1-d087-b3bb-846a8555xxxx","xxxxxxxx-ceb1-d087-b3bb-846a8555xxxx"]

which means it is a JSON array and in this case has 2 entries.   In order to really use this field you would use mv-expand on the column as in 

 

SecurityIncident
| mv-expand AlertIds

 

This will create a new row for each entry in the AlertIds column.  All the other columns will be the same but the AlertIds column will only contain a single value per row.  This makes it much easier to perform a join against the SecurityAlert table to get the alert information.

@CodnChips 

 

You can get some code examples from the Sentinel Github, via the search box, then filter on YAML files (to reduce some noise, the json files can also be useful...but start simple!).  

Clive_Watson_0-1643981702869.png

You should end up with something like:

 

https://github.com/Azure/Azure-Sentinel/search?l=YAML&q=mv-expand

 

Another example of its use is where you have [0] and [1] type entries (often these are 10 or more).

Clive_Watson_1-1643981979440.png

 

 You can use mv-expand to produce the data without the [numbers]

 

Clive_Watson_2-1643982084028.png

 

You can play with this example in the Logs Demo:

 

Go to Log Analytics and run query

Hi @Gary Bushey Right - I've got it thankyou.
It's much easier to see the affect on a single alert otherwise there's too much noise to see what happened!!
I've tried that with the AdditionalData field and understand what's happening thankyou.
So, to then expand on your comment RE perform a join, is this is where you're going to pull\refence\merge data from another table?
If you could bolt onto your initial example (if you get time\have the inclination), then it would be greatly apprecaited, thanks

Hey @Clive_Watson thanks very much for your response. This is so helpful. I will have a look on the Github link and see what I can find as next step as Gary eluded to is connecting with a Join.
Many thanks

@CodnChips 

 

https://github.com/Azure/Azure-Sentinel/search?l=YAML&q=mv-expand+AlertIds

 

Note: I searched for mv-expand and AlertIds, now you have two real examples.

Extract of part of one of these example queries:

 

  SecurityIncident
  | summarize hint.strategy = shuffle arg_max(LastModifiedTime, *) by IncidentNumber
  | mv-expand AlertIds
  | extend AlertId = tostring(AlertIds)
  | join kind= innerunique ( 
            SecurityAlert 
            )
            on $left.AlertId == $right.SystemAlertId
  | summarize hint.strategy = shuffle arg_max(TimeGenerated, *), NumberOfUpdates = count() by SystemAlertId

  

@Clive_Watson Thanks Clive - I've got to start smaller! I'm like rabbit in the headlights with those examples!! :D
That's interesting - on one of the examples, it shows you can do "nested" mv-expands
now you are past the rabbit in the headlight stage I see ;)
:) Yes - curiosity gets the better of me!!
So If I do this:
SecurityIncident
| where TimeGenerated == "2/4/2022, 11:49:50.950 AM"
| mv-expand AdditionalData
| project AdditionalData
I get a single nice clean column of the expanded data.

Sometimes, I see a =tostring in the examples, eg:
SecurityIncident
| where TimeGenerated == "2/4/2022, 11:49:50.950 AM"
| mv-expand AdditionalData
| project AdditionalData = tostring(AdditionalData)

It makes no difference to the output that I see, so what is happening?



@CodnChips 

 

it could make a difference later on, try these three examples to see the difference

 

SecurityIncident 
| limit 1
| mv-expand AdditionalData 
| project AdditionalData 
| getschema 

SecurityIncident 
|limit 1
| mv-expand AdditionalData 
| project AdditionalData = tostring(AdditionalData) 
| getschema 

SecurityIncident 
| limit 1
| mv-expand AdditionalData to typeof(string) 
| project AdditionalData 
| getschema 

 

"later on" you may need the column as a string (maybe as part of a summarize), in other cases you need it to remain dynamic - maybe you need to find a position in the array or the length etc... 

@clivewatsonQuorumCyber - Yes I see the difference and think I get what's happening. Hopefully I'll shortly arrive with a work example where I need to do this. Thanks again for all your help & patience today.
@Clive_Watson
Final one, if you get chance:
If I enter this:
SecurityAlert
| where TimeGenerated == "2022-02-06T21:33:52.77Z"
| mv-expand Entities

I get this error:
Operator mvexpand: expanded expression expected to have dynamic type

Is this because the Entities Field is a "dynamic Array"? What is it expecting?

@CodnChips 

 

Its a string, and mv-expand needs a Dynamic array - getschema will confirm this for you:

 

SecurityAlert
| getschema 
| where ColumnName =="Entities"

 

Clive_Watson_0-1644229886897.png

mv-expand operator - Azure Data Explorer | Microsoft Docs
Expands multi-value dynamic arrays or property bags into multiple records.

So you need to switch the string to Dynamic for mv-expand to work

SecurityAlert
| limit 1
| mv-expand todynamic(Entities)

 

And at that point you decide if you need the results as Dynamic or if you need them as a string (and that depends on the commands, if any, you plan to run on the filtered data)

SecurityAlert
| limit 1
| mv-expand todynamic(Entities) //to typeof(string)
| getschema 
| where ColumnName =="Entities"

 

@Clive_Watson
Aaaah - see that makes sense. The help files\docs just don't present the information in a digestible format. Thankyou so much for your clear responses - I wouldn't have extracted that understanding from the docs (Which I appreciate is my own shortcoming) :)
Glad to help ;)

You can also try https://github.com/rod-trent/MustLearnKQL for some more tips, and https://github.com/rod-trent/AddictedtoKQL when it releases.

#MustLearnKQL #KQL
@Clive_Watson Perfect, thanks!