I'm not an expert on KQL, but I have fixed the query in your example. It didn't run on my environments.
See here:

customEvents

| where timestamp between (ago(1d) .. now())

| where name in ("BatchTaskStart", "BatchTaskFinished", "BatchTaskFailure")

| extend CustomDimensionsParsed = parse_json(customDimensions)

| extend InfoMessageParsed   = parse_json(tostring(CustomDimensionsParsed.InfoMessage))

| extend ActivityId          = tostring(CustomDimensionsParsed.activityId)

| extend ClassName           = tostring(CustomDimensionsParsed.ClassName)

| extend BatchJobId          = tostring(CustomDimensionsParsed.BatchJobId)

| extend BatchJobTaskId1     = tostring(CustomDimensionsParsed.BatchJobTaskId)

| extend BatchJobTaskId2     = tostring(CustomDimensionsParsed.BatchTaskId)

| extend BatchJobTaskId      = iif(isnotempty(BatchJobTaskId1), BatchJobTaskId1, BatchJobTaskId2)

| extend StartTime  = iff(name == "BatchTaskStart", timestamp, datetime(null))

| extend EndTime    = iff(name == "BatchTaskFinished", timestamp, datetime(null))

| extend ErrorTime  = iff(name == "BatchTaskFailure", timestamp, datetime(null))

| extend RetryCount = iff(name == "BatchTaskStart", tostring(InfoMessageParsed.RetryCount), "")

| project StartTime, EndTime, ErrorTime, RetryCount, ActivityId, ClassName, BatchJobId, RoleInstance = cloud_RoleInstance, BatchJobTaskId

| where isnotempty(BatchJobId)

| summarize 

    StartTime      = min(StartTime),

    CompletionTime = max(EndTime),

    ErrorTime      = max(ErrorTime),

    RetryCount     = take_any(RetryCount),

    ClassName      = any(ClassName),

    BatchJobId     = any(BatchJobId),

    BatchJobTaskId = any(BatchJobTaskId),

    RoleInstance   = any(RoleInstance)

by ActivityId

| where isnotempty(StartTime)

| extend BatchAOS = tostring(split(RoleInstance, "-")[0])

| project ActivityId, BatchAOS, BatchJobId, BatchJobTaskId, ClassName, StartTime, CompletionTime, ElapsedTime = CompletionTime - StartTime, RetryCount