The workflow you describe sounds like what you would do if you were to perform these steps manually. There's nothing wrong with that, but we can accomplish this same goal more efficiently with KM, like so:
Create folder "Y" in parent directory
Move all items in designated folder with "XX" in the filename to folder "Y"
Using the desktop as the parent directory, here's an example macro that shows how this can work:
All you have to do is change the name of the new folder and remove or comment-out the move to parent folder line.
I've added a newFolderName property and commented-out the move to parent line in the following AppleScript:
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/09/07 10:02
# dMod: 2022/10/07 00:04
# Appl: Finder
# Task: Enclose selected item in a new named folder, and move them to the parent folder.
# Libs: None
# Osax: None
# Tags: @ccstone, @Applescript, @Script, @Finder, @Enclose, @Selected, @Item, @New, @Named, @Folder, @Move, @Parent
--------------------------------------------------------
property newFolderName : "Your New Folder Name"
--------------------------------------------------------
try
tell application "Finder"
set finderSelectionList to selection as alias list
set parentFolder to parent of (get item 1 of finderSelectionList)
set newFolder to make new folder at parentFolder with properties {name:newFolderName}
move finderSelectionList to newFolder
# move newFolder to parent of parentFolder
end tell
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell application (path to frontmost application as text) to set ddButton to button returned of ¬
(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to e
end try
end if
end try
--------------------------------------------------------
If you want to use Keyboard Maestro's native actions to do this you'll need.