How are you placing the cubes?
If you are placing the cubes from isometric coordinates (ix,iy,iz) the diagonal would just be iy.
As an example here is the math to project an isometric position (ix,iy,iz) to the screen (x,y):
X=ix-iy
Y=(ix+iy)/2-iz
If instead you only have the xy positions of the cubes you can reverse the formulas above to get the iso position. But the iz value can be anything per cube.
ix=y+x/2+iz
iy=Y-x/2+iz
Possibly you can look at the zorder and relative position of overlapping cubes to calculate the iso position too. But you’ll often have ambiguous solutions. For example if there isn’t a cube at the center intersection then it out guess four iy layers instead of three.